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:
Is there an alternative to the CreateObject command that I should be using?
Thanks for any suggestions!
Ken
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:
- 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.
- 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).
- I am running Word and Excel for Mac 2011.
- 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