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
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