Copy Cell value from Excel and paste into current email

Status
Not open for further replies.

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
Hi,

I'm very new to VBA, I've just started working with Excel VBA and finding it incredibly useful but this is the first time I've tried anything in outlook and I'm stuck already on something I hoped was just very small and simple...

I have an excel sheet that contains a unique number that I need to change anytime someone runs any one of various Macros I have in different Excel sheets. What I now want to do is create a macro for Outlook that will open the excel file increase the cell value by 1 copy the cell contents paste this into the current active email without losing any of its contents and then save and close the excel file.

Also I need it to work in both Outlook 2007 & Outlook 2003

So far I have

Code:
Sub test()
   
  Workbooks.Open ("E:\Users\Cupid Stunt\Desktop\CurrentNumber.xlsx")
  Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value = Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value + 1
  Workbooks("CurrentNumber.xlsx").Save
  Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Copy
  'Workbooks("CurrentNumber.xlsx").Close
   
End Sub
This copies the data I need but I can't find a way to paste it into the active email.
I can only paste it manually
Also I've had to quote out the last line
Workbooks("CurrentNumber.xlsx").Close
because as soon as this runs I lose the data copied to the clipboard.

Any assistance anyone could offer would be most appreciated.

Thanks
 

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
Thanks for this!, i didn't even know where to start.

I've adapted so i can now paste my required text but do you know if there is way to paste into the active email without wiping out the rest of the text that is already in the email?
 

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
In case anyone else is looking for something similar, below is what I have come up with

I used Late binding so no need to reference Word or Excel

Code:
Sub PasteCurrentNumber()

    Dim Xl As Object
    Set Xl = CreateObject("Excel.Application")
    Dim WD As Object
    Set WD = CreateObject("Word.Application")

    Dim Doc As Object
    Set Doc = Application.ActiveInspector.WordEditor
    Dim Ws As Object
    Dim Xltxt As String
 
    Xl.Workbooks.Open ("E:\Users\Cupid Stunt\Desktop\CurrentNumber.xlsx")


    Set Ws = Xl.Workbooks("CurrentNumber.xlsx").Worksheets(1)
    Xltxt = Ws.Range("A1").Value
 
    Xl.DisplayAlerts = False
 
    Xl.Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value = _
    Xl.Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value + 1
    Xl.Workbooks("CurrentNumber.xlsx").SaveAs ("E:\Users\Cupid Stunt\Desktop\CurrentNumber.xlsx")

    Doc.Application.Selection.TypeText Xltxt

    Xl.Workbooks("CurrentNumber.xlsx").Close
    Xl.DisplayAlerts = True

End Sub
 

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
Hi All,

my above Marco works fine in Outlook 2007 but when i tried to use on 2003 i got an error on the line

Code:
Doc.Application.Selection.TypeText Xltxt
Unfortunatly i don't have the error code.

does anyone have any ideas on how to fix to work in 2003 also?
 

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
Are you using word as the editor? That will be required.
Thank you Diane! Macro works great now.

But now i have another problem, i need to assign the Marco to a button and add it to one of the ribbons.

Which i could do when i wasn't using word as the editor but now since word is the editor when i try to add the macro to the ribbon i cannot see any Macros from Outlook as it's looking for Macros in Word.

Is there a solution to this?
 

Diane Poremsky

Senior Member
Best answers
0
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You can't add button to the ribbon in 2007 - you'll need to add it to the QAT.
[DOUBLEPOST=1426767995][/DOUBLEPOST]Oops, I forgot this is for 2003. I'll have to boot a 2003 system and refresh my memory.
 

Cupid Stunt

Member
Best answers
0
Outlook version
Outlook 2007
Email Account
Exchange Server
I'll have to boot a 2003 system and refresh my memory.
I Know! I've had to uninstall 2010 and re-install 2003 on my home laptop to try to get this working.
Unfortuantly we have some very large Access files that don't work with anything newer than 2003 and we currently don't have time or knowledge to update them to work with anything else.

Ok so i've read that its not possible to add an outlook Macro button when you're using word as the editor in 2003 you have to re-write the macro in word and then add it as a word macro.

so i've taken the code and pasted into Word VBA editor.

This allows me to create a macro button but the code doesn't run in word.

And while typing this it just hit me and i've got it working

re-written code below works in 2003 word editor

Code:
Sub PasteCurrentNumber()

    Dim Xl As Object
    Set Xl = CreateObject("Excel.Application")
    Dim WD As Object
    Set WD = CreateObject("Word.Application")

    Dim Ws As Object
    Dim Xltxt As String
    Xl.Workbooks.Open ("E:\Users\Cupid Stunt\Desktop\CurrentNumber.xlsx")


    Set Ws = Xl.Workbooks("CurrentNumber.xlsx").Worksheets(1)
    Xltxt = Ws.Range("A1").Value
    Xl.DisplayAlerts = False
    Xl.Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value = _
    Xl.Workbooks("CurrentNumber.xlsx").Sheets("Sheet1").Range("A1").Value + 1
    Xl.Workbooks("CurrentNumber.xlsx").SaveAs ("E:\Users\Cupid Stunt\Desktop\CurrentNumber.xlsx")

    Application.Selection.TypeText Xltxt

    Xl.Workbooks("CurrentNumber.xlsx").Close
    Xl.DisplayAlerts = True

End Sub
Thank you both very much for your guidance.

Just 1 more question

i've seen people sometimes add a couple of "tidy up" lines

like
Code:
set Xl = nothing
Is there an advantage to doing this?
 

Michael Bauer

Senior Member
Best answers
0
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
No in most cases. In your case, the variable is declared within a function and will be set to Nothing automatically as soon as the End Sub line is executed.
 
Status
Not open for further replies.
Top