Run-time error 5853 when using CreateObject in Excel VBA to create Word Object

kwoychesko

New Member
Joined
Aug 21, 2017
Messages
5
Hello,

I have some pretty basic Excel VBA code that creates an instance of Word. I am getting the following error when quitting the Word application (either manually or with code):
Run-time error '5853':
Invalid Parameter

This error appears when this line in the code below executes:
wdApp.Application.Quit

Notes:
  1. Even if I comment out the above line and quit Word manually after the code is finished executing, the error message appears when I quit Word.
  2. If Word is already open when I run the code, the error does not appear (because in that case we end up executing the GetObject command instead of the CreateObject).
  3. I am running Word and Excel for Mac 2011.
  4. If I step through the code, the error message doesn't appear! I tried putting a delay of 10 seconds before the wdApp.Application.Quit command but it didn't make a difference, the error still appeared.

Is there an alternative to the CreateObject command that I should be using?

Thanks for any suggestions!
Ken

Code:
    Dim wdApp As Object
    Dim wdDoc As Object
 
    'We need to continue through errors since if Word isn't open the GetObject line will give an error
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    
    'We've tried to get Word but if it's nothing then it isn't open
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    
    On Error GoTo 0
    
    Set wdDoc = wdApp.Documents.Add    
    wdDoc.Close SaveChanges:=False    
    wdApp.Application.Quit    'after this line executes the run-time error is displayed
    Set wdDoc = Nothing
    Set wdApp = Nothing
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hello Forum!
Can I ask: was this a dumb/obvious question, or just too hard to answer in a forum environment?
Thanks!
Ken
 
Upvote 0
was this a dumb/obvious question, or just too hard to answer in a forum environment?
None of the above, whilst there are some members who run Macs, I suspect that the vast majority are windows based.
That said I've always used
Code:
wdApp.Quit
which works quite happily in windows.
 
Upvote 0
That said I've always used
Code:
wdApp.Quit
which works quite happily in windows.

Thanks for the suggestion! It's funny, I get this same error when using that command, too -- but only when "running freely"... if I step through the code the error doesn't happen either with wdApp.Quit or wdApp.Application.Quit.
 
Upvote 0
Not sure if this is possible on a Mac but try adding a reference to Microsoft Word xx Object Library and then use something like this
Code:
Sub Ed_MailMerge()

    Dim WrdApp As Word.Application
    
    On Error GoTo OpenWrd
    Set WrdApp = Word.Application
    On Error GoTo 0


OpenWrd:
[COLOR=#ff0000]    Set WrdApp = New Word.Application[/COLOR]
    Resume Next

End Sub
 
Upvote 0
Thank you for the suggestion -- I learned quite a bit from implementing it! The same error occurred, but I did find the cause of the problem: Adobe Acrobat had added an add-in to my Word installation (linkCreation.dotm). Once I deleted that (it was like herpes: just kept coming back...) the code worked fine!
Thanks again for your help!
Ken
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top