VBA Application Close Issue

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
Thanks Yongle!

I can't wait to try it!!

- Paul
 
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,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

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