shieldsco
asked on
Need to fix Excel VBA Run Time Error 287 - application defined or object define error
I'm using the below code in Excel and receiving run-time error 287 - application defined or object define error. The error is on line :
Set wdDoc = Email.GetInspector.WordEditor
Public Sub Example()
' add ref - tool -> references - > Microsoft Outlook XX.X Object Library
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim Email As Outlook.MailItem
Set Email = olApp.CreateItem(0)
' add ref - tool -> references - > Microsoft Word XX.X Object Library
Dim wdDoc As Word.Document '<=========
Set wdDoc = Email.GetInspector.WordEditor
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.Worksheets("Pivot (2)")
Dim rng As Range
Set rng = Sht.Range("A3:D50").SpecialCells(xlCellTypeVisible)
rng.Copy
With Email
.To = Sht.Range("F4")
.Subject = Sht.Range("J5")
.Display
wdDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
End Sub
The error normally means that one of the references - Email or GetInspector - is Nothing.
GetInspector is not a property or method of Word.Email.
Email object (Word)
Email object (Word)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for letting me know. I will try your suggestion.
It might help to try repairing MS Office. And sometimes a reboot "fixes" weird vba behavior.
Also I suspect your problem is actually in Outlook. If I am right, you should first do trouble shooting, by moving the problem code out of Excel and into Outlook. Once it is working under Outlook, move the working code back into Excel. For example, the following works under my Outlook 2010 installation.
You not need to use Tools > References because the Outlook's default 4 references are sufficient. (Office 14, Outlook 14,Visual Basic; OLE & OLE). You will notice that I have changed several dim statements to be As Object (hich is Late Binding. I like early binding because the vba ide smart dropdowns supports it nicely. But I often revert to late binding (As Object) when troubles arise. Once things are working I sometimes revert to early binding.
Also I suspect your problem is actually in Outlook. If I am right, you should first do trouble shooting, by moving the problem code out of Excel and into Outlook. Once it is working under Outlook, move the working code back into Excel. For example, the following works under my Outlook 2010 installation.
You not need to use Tools > References because the Outlook's default 4 references are sufficient. (Office 14, Outlook 14,Visual Basic; OLE & OLE). You will notice that I have changed several dim statements to be As Object (hich is Late Binding. I like early binding because the vba ide smart dropdowns supports it nicely. But I often revert to late binding (As Object) when troubles arise. Once things are working I sometimes revert to early binding.
Option Explicit
Public Sub Example()
' add ref - tool -> references - > Microsoft Outlook XX.X Object Library
Dim olApp As Outlook.Application
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
MsgBox "please open outlook before running this routine"
Exit Sub
End If
On Error GoTo 0
Dim Email As Outlook.MailItem
Set Email = olApp.CreateItem(0)
Email.Display
' add ref - tool -> references - > Microsoft Word XX.X Object Library
Dim insp As Object
Set insp = olApp.ActiveInspector
Dim wdDoc As Object '<=========
Set wdDoc = insp.WordEditor
MsgBox "wdDoc has been created. Its TypeName is " & TypeName(wdDoc)
Exit Sub
#If False Then
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.Worksheets("Pivot (2)")
Dim rng As Range
Set rng = Sht.Range("A3:D50").SpecialCells(xlCellTypeVisible)
rng.Copy
With Email
.To = Sht.Range("F4")
.Subject = Sht.Range("J5")
.Display
wdDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
#End If
End Sub
ASKER
Thanks