Determine if word document is open

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
I am using Excel 2003 to create word documents. The starter word doc has codes that get replaced and I then do a save-as

When the starter document is already open, it gets corrupted when I try to open it with VB.

How can I determine if a specific word document is already open?

Here is some of the code to open it

Code:
Dim oWordApp As Object, openDoc as string
Set oWordApp = CreateObject("Word.Application")

'  check if already open -------------

openDoc = "C:\Temp\Start.doc

oWordApp.Documents.Open openDoc
oWordApp.Visible = True

Any advice appreciated
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This code traps the error produced by GetObject if Word is not open.
Code:
    '---------------------------------------
    On Error Resume Next
    '- try to get Word application
    Set xlApp = GetObject(, "Word.Application")
    If Err.Number Then ' Word is not open
        Set xlApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    '-------------------------------------------------
 
Upvote 0
Thanks for the answer, however your logic checks whether the "Word Apllication" is open.

I want to determine if the actual word document that I want to open is already opened by the user.
 
Upvote 0
Code:
Function IsWordDocOpen(sWordDocFullName As String) as Boolean
    Dim wdApp as Object ' Late Binding - who cares?
    Dim wdDoc as Object

    IsWordDocOpen = False

    On Error Resume Next 
    '- try to get Word application 
    Set wdApp = GetObject(, "Word.Application") 
    If Err.Number Then ' Word is not open 
        Exit Function
    End If 
    On Error GoTo 0

    For Each wdDoc in wdApp.Documents
        If wddoc.FullName = sWordDocFullName Then
            IsWordDocOpen = True
            Exit Function
        End If
    Next

End Function
 
Upvote 0
Apologies to you John, did not see the reply.

Will implement tonight (it is a private project), will let you know tomorrow
 
Upvote 0
Jon,

I've tried your function and have a few questions as I need to be able to use the same snippit of code.

1. You have it setup as a function; can it be called from another macro or do I need to make it a sub?

2. The For loop at the end where it cycles through all word documents doesn't run. I step up to the first line and it skips completely out. Is there anything else that needs to be set so this will work? I already have the reference for Word added. I've tried a similar bit of code from someone else and the same problem happens where it skips the entire FOR loop.
 
Upvote 0
1. Call it as you would call any function from another procedure:

Dim bWordTest as Boolean
bWordTest = IsWordDocOpen("C:\temp\MyDoc.doc")

2. If the loop doesn't loop, it means there are no documents open.
 
Upvote 0
Thanks.

I've tweaked it a bit to suit my needs (if word isn't open it opens it) but it comes back saying "Argument not optional" on the line I've marked. Is there a reason I wouldn't be able to fire up an instance of word here?

Code:
Sub ListPictureExport()

Dim WDApp As Object
Dim WDDoc As Object
Dim DocName As String
Dim DocPath As String
Dim FullDoc As String
Dim ReadyFlag As Boolean


'check if export list exists
On Error Resume Next
Application.Goto Reference:="HelperStart"
If Err.Number = 1004 Then
    MsgBox "export list doesn't exist"
    Exit Sub
End If
On Error GoTo 0

                                                                                                        
'Get file info
DocName = ActiveCell.Value & ".doc"
DocPath = ActiveCell.Range("A2").Value
FullDoc = DocPath & DocName
                                                    
'Check if Word is open
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number Then
    Set WDApp = CreateObject(, "Word.Application")    <--ERROR SHOWS UP HERE
    WDApp.Visible = True
End If
On Error GoTo 0

'Check if Document is open
ReadyFlag = IsWordDocOpen(FullDoc)

If Not (ReadyFlag) Then GoTo quickend


'etc etc




quickend:
Set WDApp = Nothing
Set WDDoc = Nothing
End Sub
 
Upvote 0
Take out the comma in this line only (not in GetObject):

Code:
    Set WDApp = CreateObject("Word.Application")
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,215
Members
448,874
Latest member
b1step2far

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