Looping thru open word documents to find a specific one

D

dsimcox

My Excel VBA code checks to see if a specific word file is open.

If I have more than one inistance of Word open, my code only "sees" on
instance.

How can I loop through all the instances of Word that are open to find the
specific document I want to act on?

=====================================================

Private Sub cmdOpenPRRRDoc_Click()

Dim WordApp As Object
Dim fNameAndPath As String
Dim doc As Variant, x As Integer

On Error Resume Next
fNameAndPath = GetPRRRFileName


' CHECK TO DETERMINE IF THE FILE IS ALREADY OPEN
' IF NOT OPEN - THEN OPEN IT
If Not FileLocked(fNameAndPath) Then
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Open (fNameAndPath)
WordApp.Visible = True
Set WordApp = Nothing
Exit Sub
End If

' IF OPEN - THEN ACTIVATE IT
For Each doc In Documents
If InStr(1, fNameAndPath, doc.Name, vbTextCompare) > 0 Then
doc.Activate
End If
Next

' IF MORE THAN ONE INSTANCE OF WORD IS OPEN, I MAY MISS THE FILE IN MY
FOR-EACH LOOP

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top