VBA to close open Word document

oseitutuSamuel

Board Regular
Joined
Jul 31, 2015
Messages
82
Hi People,

I use the function below in Excel VBA to open different Word documents one at a time. I pass the Word document's name as a parameter to this function to open it.
Please kindly show me how I can write another code to close a document that is already opened before I open another one.

Many thanks:

Public Function FnOpenWordDoc(strWordDoc)
Dim wordApp As Word.Application
Set wordApp = CreateObject("Word.Application")
wordApp.Documents.Open (strWordDoc)
wordApp.Application.WindowState = wdWindowStateMaximize
wordApp.ActiveWindow.ActivePane.View.Zoom.Percentage = 100
wordApp.Visible = True
wordApp.Activate
End Function
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Something like this :
Code:
Public Function FnOpenWordDoc(strWordDoc) As Object
    Dim wordApp As Object
    
    On Error Resume Next
        Set wordApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wordApp Is Nothing Then
        Set wordApp = CreateObject("Word.Application")
    End If
    With wordApp
        Set FnOpenWordDoc = .Documents.Open(strWordDoc)
        .WindowState = wdWindowStateMaximize
        .ActiveWindow.ActivePane.View.Zoom.Percentage = 100
        .Visible = True
        .Activate
    End With
End Function

And then when using the function, try something along these lines :
Code:
Sub test()

    Dim oDoc As Object
    
    Set oDoc = FnOpenWordDoc("D:\MyWordDoc.docm")

    
    [COLOR=#008000][B]' Do your stuff here ....[/B][/COLOR]
    

    [B][COLOR=#008000]'Close the Word document.[/COLOR][/B]
    If Not oDoc Is Nothing Then
        oDoc.Close  [B][COLOR=#008000]'<==optionally Set the savechanges arg to True before closing the word document[/COLOR][/B]
    End If
End Sub
 
Last edited:
Upvote 0
Alternatively, something like:
Code:
Public Function FnOpenWordDoc(strWordDoc)
Dim wdApp As New Word.Application, wdDoc As Word.Document
With wdApp
  .Visbible = False
  Set wdDoc = .Documents.Open(Filename:=strWordDoc, AddToRecentFiles:=False)
  With wdDoc
    'do stuff, then
    .Close SaveChanges:=True
  End With
  .Quit
End With
End Function
There is no particular reason to mess with Word's display setting, or even have it visible, if all you're doing is editing a document before closing it with saved changes.
 
Upvote 0
Hello Jaafar & Macropod,
Many thanks for taking your time to help with my code. I am truly grateful. I spent many hours on it but couldn't get it working.
Between you and Macropod, you have "Killed the beast"

Regards,
Samuel
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

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