battery514
08-30-2016, 07:27 AM
I am having a hard time with this macro, and at this point I feel like I have searched everywhere, and there doesn't seem to be a lot of good Outlook VBA info out there. I have updated the macro to the below code, which correctly opens a new Excel workbook, however, it does not copy the body of the email into said workbook. Obviously, there is an issue with my copy and paste methodology, and I have also tried Send Keys with no avail. Any ideas are greatly appreciated.
Option Explicit
Sub PasteToExcel(item As Outlook.MailItem)
Dim activeMailMessage As MailItem
Dim xlApp As Excel.Application
Dim Wb As Excel.Workbook
Dim Ws As Excel.Worksheet
On Error Resume Next
'Get a handle on the email
Set activeMailMessage = ActiveExplorer.Selection.item(1)
'Copy the formatted text:
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'Ensure Excel Application is open
Set xlApp = CreateObject("Excel.Application")
'Make Excel Application visible
xlApp.Visible = True
'Open the Personal Macro Workbook, or the Excel macro won't run
xlApp.Workbooks.Open ("C:\Users\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.xlsb")
'Name the Excel File
Set Wb = xlApp.Workbooks.Add
'Paste the email
Set Ws = xlApp.Sheets(1)
Ws.Activate
Ws.Range("A1").Paste
'Run the Excel macro to clean up the file
xlApp.Run ("PERSONAL.XLSB!Commissions_Report_Format")
End Sub
Option Explicit
Sub PasteToExcel(item As Outlook.MailItem)
Dim activeMailMessage As MailItem
Dim xlApp As Excel.Application
Dim Wb As Excel.Workbook
Dim Ws As Excel.Worksheet
On Error Resume Next
'Get a handle on the email
Set activeMailMessage = ActiveExplorer.Selection.item(1)
'Copy the formatted text:
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'Ensure Excel Application is open
Set xlApp = CreateObject("Excel.Application")
'Make Excel Application visible
xlApp.Visible = True
'Open the Personal Macro Workbook, or the Excel macro won't run
xlApp.Workbooks.Open ("C:\Users\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.xlsb")
'Name the Excel File
Set Wb = xlApp.Workbooks.Add
'Paste the email
Set Ws = xlApp.Sheets(1)
Ws.Activate
Ws.Range("A1").Paste
'Run the Excel macro to clean up the file
xlApp.Run ("PERSONAL.XLSB!Commissions_Report_Format")
End Sub