ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping thru open word documents to find a specific one (https://www.excelbanter.com/excel-programming/395061-looping-thru-open-word-documents-find-specific-one.html)

dsimcox

Looping thru open word documents to find a specific one
 
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

Smallweed

Looping thru open word documents to find a specific one
 
This ain't easy and involves API programming (window handlers, etc). Here's
a couple of references to get you started:

http://www.informit.com/articles/art...&seqNum=3&rl=1
http://www.bmsltd.ie/Spreads/ProExcelDev09.pdf

"dsimcox" wrote:

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



All times are GMT +1. The time now is 06:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com