H
Hii Sing Chung
Dear experts,
I am writing VBA in Excel to try to automate the daily reporting of sending
data from Excel through e-mail. I got stuck at getting Excel to paste the
clipboard data into the right place in e-mail. The codes:
Sub SendDailyReport()
Dim objOutlook As Outlook.Application
Dim Doc As Word.Document
Dim wdRange As Word.Range
Dim objEmail As Outlook.MailItem
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Application.ScreenUpdating = False
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = New Outlook.Application
objInbox.Display
End If
Set wbBook = ThisWorkbook
Set wsSheet = ThisWorkbook.Worksheets("reporting")
wsSheet.Activate
With ActiveSheet.UsedRange
..Copy
End With
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
..Importance = olImportanceHigh
..Subject = "Daily Report " & FormatDateTime(Date, vbLongDate)
..To = "Senior Managers"
..Body = "Dear Sir, " & vbCrLf & vbCrLf & _
"Daily Report As Show Below:" & vbCrLf & vbCrLf & vbCrLf & _
vbCrLf & vbCrLf & "Thanks and Best Regards," & vbCrLf & vbCrLf & _
"Security Group" & vbCrLf & "Extension Number: 27555"
..Display
Set Doc = objEmail.GetInspector.WordEditor
Set wdRange = Doc.Range
wdRange.Paste
End With
Set wsSheet = ThisWorkbook.Worksheets("chart")
wsSheet.Range("A19:AD59").Copy
objEmail.Display
wdRange.Paste
Application.CutCopyMode = False 'clear the clipboard
ExitSub:
Set objNewMail = Nothing
Set objInbox = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
Exit Sub
As of now, the paste into e-mail will overwrite whatever is already there. I
want the first paste (from worksheet "reporting") to paste right after the
"Daily Report As Show Below:" line and the 2nd paste (from worksheet
"Chart") to paste right below the first pasted data.
How do I manipulate the range or cursor so that I can paste at the right
place? Any help is very much appreciated.
I am writing VBA in Excel to try to automate the daily reporting of sending
data from Excel through e-mail. I got stuck at getting Excel to paste the
clipboard data into the right place in e-mail. The codes:
Sub SendDailyReport()
Dim objOutlook As Outlook.Application
Dim Doc As Word.Document
Dim wdRange As Word.Range
Dim objEmail As Outlook.MailItem
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Application.ScreenUpdating = False
Set objOutlook = GetObject(, "Outlook.Application")
If objOutlook Is Nothing Then
Set objOutlook = New Outlook.Application
objInbox.Display
End If
Set wbBook = ThisWorkbook
Set wsSheet = ThisWorkbook.Worksheets("reporting")
wsSheet.Activate
With ActiveSheet.UsedRange
..Copy
End With
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
..Importance = olImportanceHigh
..Subject = "Daily Report " & FormatDateTime(Date, vbLongDate)
..To = "Senior Managers"
..Body = "Dear Sir, " & vbCrLf & vbCrLf & _
"Daily Report As Show Below:" & vbCrLf & vbCrLf & vbCrLf & _
vbCrLf & vbCrLf & "Thanks and Best Regards," & vbCrLf & vbCrLf & _
"Security Group" & vbCrLf & "Extension Number: 27555"
..Display
Set Doc = objEmail.GetInspector.WordEditor
Set wdRange = Doc.Range
wdRange.Paste
End With
Set wsSheet = ThisWorkbook.Worksheets("chart")
wsSheet.Range("A19:AD59").Copy
objEmail.Display
wdRange.Paste
Application.CutCopyMode = False 'clear the clipboard
ExitSub:
Set objNewMail = Nothing
Set objInbox = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
Exit Sub
As of now, the paste into e-mail will overwrite whatever is already there. I
want the first paste (from worksheet "reporting") to paste right after the
"Daily Report As Show Below:" line and the 2nd paste (from worksheet
"Chart") to paste right below the first pasted data.
How do I manipulate the range or cursor so that I can paste at the right
place? Any help is very much appreciated.