VBA Application Close Issue

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Dear All,

If you run this application, and this opens the xlMicrsoftWord ..... how do you get VBA to CLOSE it??


Code:
Sub Macro1()
'


Application.ActivateMicrosoftApp xlMicrosoftWord



End Sub

Thank you,
Pinaceous
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this
Code:
Sub CloseWord()
    Dim W As Object
    On Error Resume Next
    Set W = GetObject(, "Word.Application")
    If W Is Nothing Then Exit Sub
    W.Quit
    Set W = Nothing
End Sub
 
Last edited:
Upvote 0
Short & sweet - not sure where I got this from but it does work
Code:
Sub KillWord()
    Dim a:   a = Shell("powershell.exe kill -processname winword", 1)
End Sub
 
Upvote 0
Hello Yongle,

I'm getting a VBA debug error when running Sub KilWord.

It pops a Run-time error '5':

Invalid procedure call or argument.


Also, in running Macro1 that I've posted #1 , when using Sub CloseWord, it will not close (?) It appears to not do anything at all.


Do you have any suggestions??

Thank you,
Pinaceous
 
Upvote 0
Tested (on Windows10 and Office 365) - both macros work for me
Powershell has been around for more than 10 years - I would expect the 2nd macro to work with Windows from Windows7 onwards

It's a bit of a mystery:confused:

Please try opening Word without using the macro in post#1 and test both of the macros I posted and let he know what happens

Which versions of Windows and Office are your running?
 
Last edited:
Upvote 0
Are you activating an already running instance of Word or opening a new one ?

Also, do you have more than one instance of Word running when executing the CloseWord macro ?

If you are creating a new instance of Word, how about using CreateObject instead of ActivateMicrosoftApp? That way, you get a pointer to the Word instance which you can later use for closing.
 
Last edited:
Upvote 0
try opening Word without using the macro in post#1
Okay, I’ve tried a different method and it seemed to do the trick.
Here’s the code that I’ve tested:
Code:
Sub CreateWord()
'
Dim objWord
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
End Sub
Sub CloseWord()
    Dim W As Object
    On Error Resume Next
    Set W = GetObject(, "Word.Application")
    If W Is Nothing Then Exit Sub
    W.Quit
    Set W = Nothing
End Sub
Sub Window()
'
Call CreateWord
Call CloseWord

End Sub

Now, I’ve noticed an issue with in running Sub CreateWord() by itself it hides behind Excel’s window.
Do you know, I can push this to dominate the Excel window? Or do you think this warrants a new thread?

Thank you,
Paul
 
Upvote 0
Jaafar,
I’m opening a single new Word document then executing the close on only that document.

I’m not sure how to CreateObject in lieu of ActivateMicrosoftApp. Could you provide an example?
 
Upvote 0
I’m not sure how to CreateObject in lieu of ActivateMicrosoftApp. Could you provide an example?

You already used CreateObject in the CreateWord routine in post#8

Now, I’ve noticed an issue with in running Sub CreateWord() by itself it hides behind Excel’s window.
Do you know, I can push this to dominate the Excel window? Or do you think this warrants a new thread?

Try this to bring word to the front:
Code:
Sub CreateWord()

    Dim objWord
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
[B][COLOR=#ff0000]    AppActivate objWord.Caption[/COLOR][/B]

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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