Iterating through open Word documents

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have a MS Word template file called "Tailored Bespoke Terms Template 2017 v4.dotm"

The code I have, opens this file. However when the code errors (I'm in the development phase right now), the file remains in memory. I'm looking for some code that will iterate through any open MS Word documents and close\release "Tailored Bespoke Terms Template 2017 v4.dotm".

I have Option Explicit in my top line of code.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Show us your code. If you create a new Word instance and make both Word and the document created from your template visible during development, they're both easy close if something goes wrong. Moreover, error-trapping should enable you to exit gracefully from various errors.

PS: Option Explicit, though good to use, has nothing in particular to do with this issue.
 
Upvote 0
This is the code I have inherited.

Code:
Dim WordApp As Object
Dim WordDoc As Word.Document

On Error Resume Next
    
Set WordApp = GetObject(, "Word.Application")

On Error GoTo ErrorHandler

If WordApp Is Nothing Then
    Set WordApp = CreateObject("Word.Application")
    AppOpenedByMac = True
    Set WordDoc = WordApp.Documents.Open(Template_Filepath)
    WordApp.Visible = True
    DocOpenedByMac = True
    GoTo OpenedByMacro
Else
    On Error GoTo NotOpen
    Set WordDoc = WordApp.Documents(Template_Name)
    WordApp.Visible = True
    GoTo OpenAlready
End If

NotOpen:
    Set WordDoc = WordApp.Documents.Open(Template_Filepath)
    DocOpenedByMac = True
    GoTo OpenedByMacro

OpenAlready:
    On Error GoTo 0
    response = MsgBox("Word template for Terms and Conditions is already open. It may not be possible to undo changes." & vbCr & vbCr & "Continue?", vbOKCancel, "Word template already open")
    If response = vbCancel Then
        ProgMsg.Hide
        Exit Sub
    End If

OpenedByMacro:

ProgMsg.ProgText = "Microsoft Word is working..." & vbCr & vbCr & "Time taken: " & Second(Now - TimeNow) & " seconds"
ProgMsg.ProgBar.Width = 200

'Call Word macro to set T&C's
WordApp.Run "Set_T_Cs", Policy_Info, Benefits_Included, TUs_Info, Colour_Info
'WordApp.Run "Set_T_Cs", Policy_Info, Benefits_Included, TUs_Info

Application.Wait (Now + TimeValue("00:00:02"))

If DocOpenedByMac = True Then
    WordDoc.Close (wdDoNotSaveChanges)
End If

If AppOpenedByMac = True Then WordApp.Quit SaveChanges:=wdDoNotSaveChanges

Set WordApp = Nothing

ErrorHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
'        Resume Next
    End If

End Sub

If the code further back errors for some reason, the Word template remains and doesn't even appear in the Task Manager. It's only if I open a blank Word document and go into the code window I can see that's it's still there.

If I can iterate through all the open Word files and close the offending file.
 
Last edited:
Upvote 0
If the code further back errors for some reason, the Word template remains and doesn't even appear in the Task Manager. It's only if I open a blank Word document and go into the code window I can see that's it's still there.
That does not indicate that Word continued running in the background. What it indicates is that, when you re-started Word after the crash, it attempted to recover your previous session in accordance with your auto-recover settings.
 
Upvote 0
It seems to be behaving itself now as I have managed to resolve the errors.
 
Upvote 0

Forum statistics

Threads
1,223,762
Messages
6,174,351
Members
452,556
Latest member
Chrisolowolafe

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