I am looking to bring data from an Outlook message into Access using Excel as a "middleman" for ease of referencing the data. I've been able to copy from Outlook to Excel, and import data from Excel into Access.
I'm looking to handle the entire process from Access.
The line of code, in Outlook VBA, to copy the e-mail body is not working in Access VBA. I'm thinking I have a problem with a variable dim, or set.
The error is
Run Time 287 Application-defined or object-defined error.
I can close the open e-mail with VBA from Access, so I know the applications are talking to one another.
Private Sub cmdNewFromEmail_Click()
Dim strWhere As String
Dim xl As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim filePath As String
Dim objol As Outlook.Application
Dim MyInspector As Outlook.Inspector
Dim objItem As Outlook.MailItem
Dim CurrentMessage As Outlook.MailItem
Set xl = New Excel.Application
filePath = "S:\UserNanme\" & "Excel Test File" & ".xlsx"
Set xlBook = xl.Workbooks.Open(filePath)
Set xlSheet = xlBook.Worksheets(1)
'Set OL = GetObject(, "Outlook.Application")
Set objol = New Outlook.Application
Set MyInspector = Outlook.ActiveInspector
Set objItem = MyInspector.CurrentItem
xl.Visible = True
'OL.Visible = True
DoCmd.GoToRecord , , acNewRec
'Setting up a new file in Access
Me.File_Number = Nz(DMax("File_Number", "ClaimInfo1", strWhere), 0) + 1
Me.Invoice_Number = Me.File_Number & "-01"
DoCmd.RunCommand acCmdSaveRecord
'Binds Client Billing tab to Invoicing form when setting up new file
Me.SubformContainer.SourceObject = "Appraisals_Subform2"
'Sets focus on the appropriate tab.
Forms!Invoicing_Form.TabCtlEval = 0
'---------------------------------------------------------------------
With objItem
'Set CurrentMessage = ActiveInspector.CurrentItem
' *** This is the line causing the problem.
' Other things I've tried are commented out.***
Set CurrentMessage = MyInspector.WordEditor.Range.FormattedText.Copy
'Set CurrentMessage = MyInspector
'CurrentMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'CurrentMessage.Close olSave
'CurrentMessage.GetInspector().WordEditor.Range.FormattedText
'CurrentMessage.Copy
End With
'---------------------------------------------------------------------
With xlSheet
.Range("A1").Select
.Paste
.Range("F5").Value = "Claim: "
.Range("G5").Formula = "=INDEX(B1:B100,MATCH(F5,A1:A100,0))"
End With
Me.Claim_Number = Range("G5")
End Sub
Copy) to an object variable. Just deleteSet CurrentMessage =