Hi all,
Im not the greatest with Excel VBA but i have put together a small programme to send an excel range via an Outlook email using send keys and then to return to excel and do something else.
The progamme works perfectly in isolation (Excel to Outlook -End)
But when i add excel code to the bottom (Excel to Outlook to Excel-End) the range that i have copied from excel does not paste into the Email body (Send keys ..."v"), so the email sends to the correct address, subject etc but the body is blank.
I get the impression something is clearing the clipboard or the sendkeys "v" is pasting somewhere else and i think it has something to do with adding the final Excel code at the end.
Any help would be greatly appreciated . Code below
Sub test()
Mystring1 = Range("A1")
Sheets("Sheet1").Select
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$G$502841").AutoFilter Field:=7, Criteria1:= _
Mystring1
Range("A1:G502841").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngCc As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With Sheets("Sheet3")
Set rngTo = .Range("I1")
Set rngCc = .Range("I2")
Set rngSubject = .Range("I3")
Set rngBody = .Range("A1:G10")
End With
rngBody.Copy
With objMail
.To = rngTo.Value
.Cc = rngCc.Value
.Subject = rngSubject.Value
.Display
End With
With objMail
SendKeys "^({v})", True
SendKeys "%({S})", True
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngCc = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Sheets("Sheet2").Select
Rows("1:1").Select
Selection.Delete Shift:=xlUp
End Sub
Im not the greatest with Excel VBA but i have put together a small programme to send an excel range via an Outlook email using send keys and then to return to excel and do something else.
The progamme works perfectly in isolation (Excel to Outlook -End)
But when i add excel code to the bottom (Excel to Outlook to Excel-End) the range that i have copied from excel does not paste into the Email body (Send keys ..."v"), so the email sends to the correct address, subject etc but the body is blank.
I get the impression something is clearing the clipboard or the sendkeys "v" is pasting somewhere else and i think it has something to do with adding the final Excel code at the end.
Any help would be greatly appreciated . Code below
Sub test()
Mystring1 = Range("A1")
Sheets("Sheet1").Select
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$G$502841").AutoFilter Field:=7, Criteria1:= _
Mystring1
Range("A1:G502841").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngCc As Range
Dim rngSubject As Range
Dim rngBody As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With Sheets("Sheet3")
Set rngTo = .Range("I1")
Set rngCc = .Range("I2")
Set rngSubject = .Range("I3")
Set rngBody = .Range("A1:G10")
End With
rngBody.Copy
With objMail
.To = rngTo.Value
.Cc = rngCc.Value
.Subject = rngSubject.Value
.Display
End With
With objMail
SendKeys "^({v})", True
SendKeys "%({S})", True
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngCc = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Sheets("Sheet2").Select
Rows("1:1").Select
Selection.Delete Shift:=xlUp
End Sub