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
 
Hi Jaafar,

Ok, it works on the workbook but I'm having trouble now placing this code onto my other workbook.

Could I send it to you, so you can take a look at it?

Thank you,
Paul
 
Last edited:
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
1. When opening Word from VBA I usually do everything iin the same macro - something like this...

Code:
Sub DoSomethingWord()
    Open Word
    Open document
    Modify document
    Save document  
    Close document
    Close Word
End Sub
2. You didd not tell us what you are doing in Word after opening it, only that you wanted it closing. You created 2 separate macros, one to open, and one to close Word.
If you do that, VBA needs to remember what to close after the first macro has run so that you can close it again with a different macro
- which can be done by declaring a PUBLIC variable which must be placed above all procedures in a STANDARD module

Code:
Public [COLOR=#ff0000]objWord[/COLOR] As Object                                       'ABOVE ALL PROCEDURES

Sub CreateWord()
    Set [COLOR=#ff0000]objWord[/COLOR] = CreateObject("Word.Application")
    objWord.Visible = True
    AppActivate objWord.Caption
End Sub
Sub CloseWord()
    If [COLOR=#ff0000]objWord[/COLOR] Is Nothing Then Exit Sub
    objWord.Quit
    Set objWord = Nothing
End Sub
Sub Window()
    Call CreateWord
    Call CloseWord
End Sub
3. (Dependent on what you want to do with VBA) you may also need to add the Word Object Library to the list of references (see VBA \ Tools tab \ References)

This may be helpful https://docs.microsoft.com/en-us/of...one-microsoft-office-application-from-another
 
Last edited:
Upvote 0
Here's some module code....
Code:
Public WrdObj As Object

Public Sub OpenWord()
'open Word application
On Error Resume Next
Set WrdObj = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set WrdObj = CreateObject("Word.Application")
End If
End Sub

Public Sub CloseDoc()
'close doc
Dim temp As Object
On Error Resume Next
Set temp = WrdObj.ActiveDocument
If Err.Number <> 0 Then
On Error GoTo 0
Set temp = Nothing
Exit Sub
End If
WrdObj.ActiveDocument.Close savechanges:=False
Set temp = Nothing
End Sub

Public Sub CloseWord()
'close Word application
On Error Resume Next
Set WrdObj = GetObject(, "word.application")
If Err.Number = 0 Then
WrdObj.Quit
End If
On Error GoTo 0
Set WrdObj = Nothing
End Sub
To operate (close any active document/Ensure Word is started/bring doc to foreground)….
Code:
Sub OpenSomeDoc()
Call CloseDoc
Call OpenWord
WrdObj.Visible = True
WrdObj.Documents.Open Filename:="C:\TestFolder\Test.doc" ' change file to suit
'bring Word doc to front
AppActivate WrdObj.ActiveWindow.Caption & " - " & WrdObj.Caption
WrdObj.WindowState = 1 'wdWindowStateMaximize
End Sub
To close doc and quit Word...
Code:
Sub CloseQuitWord()
Call CloseDoc
Call CloseWord
End Sub
HTH. Dave
 
Upvote 0
@Yongle

Regarding point# 2 , I would rather not store a reference pointer to the Word application in a module level variable as this can be lost.

If there happens to be only one Word application instance running then I would use GetObject otherwise I would store the pointer (using ObjPtr) in a cell or name then recover it using CopyMemory.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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