AutomateExcel
New Member
Hi,
I'm trying to write macro code to copy a range from an excel sheet (Sheet Name: IN) and paste the copied range to Outlook Email Body.
Below is the code that I have. I'm not sure what am I missing. The code is creating the email but range is not being pasted.
Please see beloe my code and the email it generates:
>>> use code - tags <<<
I'm trying to write macro code to copy a range from an excel sheet (Sheet Name: IN) and paste the copied range to Outlook Email Body.
Below is the code that I have. I'm not sure what am I missing. The code is creating the email but range is not being pasted.
Please see beloe my code and the email it generates:
>>> use code - tags <<<
Code:
Sub Copy_Range_to_Outlook_Email()
'Declare Outlook Variables
Dim oLookApp As Outlook.Application
Dim oLookItem As Outlook.MailItem
Dim oLookIns As Outlook.Inspector
'Declare Word Variables
Dim oWordDoc As Word.Document
Dim oWordRng As Word.Range
'Declare Excel Variables
Dim exclRng As Range
Dim msgText As String
On Error Resume Next
'Get the Active Instance of Outlook
Set oLookApp = GetObject(, "Outlook.Application")
'If error create a new instance of Outlook
If Err.Number = 429 Then
'Clear Error
Err.Clear
'Create new instance of Outlook
Set oLookApp = New Outlook.Application
End If
'Create a new Email
Set oLookItem = oLookApp.CreateItem(olMailItem)
'Create reference to excel range
'Set exclRng = Sheet3.Range("A1:C3")
Set exclRng = Sheets("IN").Range("A1:C3")
'Define Email Body
msgText = "Dear Reporter," & vbNewLine & vbNewLine & "Please take note of the following alerts:" & vbNewLine
With oLookItem
'Define basic information
.To = "abc@xyz.com"
.CC = "abc@xyz.com"
.Subject = "Alert on status"
.BodyFormat = olFormatRichText
.Body = msgText
.Display
'.Send
'Get the Active Inspector
Set oLookIns = .GetInspector
'Get the word editor
Set oWordDoc = oLookIns.WordEditor
'Specify range in document
Set oWordRng = oWordDoc.Application.ActiveDocument.Content
oWordRng.Collapse Direction:=wdCollapseEnd
'Add new paragraph
Set oWordRng = oWdEditor.Paragraph.Add
oWordRng.InsertBreak
'Copy range
exclRng.Copy
'Paste range to email
'oWordRng.PasteSpecial DataType:=wdPasteMetafilePicture
oWordRng.PasteSpecial
End With
End Sub
Last edited by a moderator: