PDA

View Full Version : How do I check if a word file is open from code?



Djblois
06-04-2009, 07:58 AM
I have been doing a search and can't find it? I just want to test if a document is already open and if it is switch to it. How do I do this? I know how to do it with Excel very easily.

georgiboy
06-04-2009, 11:36 AM
Maybe something like this...

You will need to set the word reference

Sub IsWordOpen()
Dim wordDoc As Word.Document
Dim wordAppl As Word.Application
Dim mydoc As String
Dim myAppl As String

mydoc = "C:\Example.docx"
myAppl = "Word.Application"


If IsRunning(myAppl) Then
MsgBox "Word is running"
Set wordDoc = GetObject(mydoc)
wordDoc.Windows("Example.docx").WindowState = wdWindowStateMaximize
End If

End Sub

Function IsRunning(ByVal myAppl As String) As Boolean
Dim applRef As Object
On Error Resume Next

Set applRef = GetObject(, myAppl)
If Err.Number = 429 Then
IsRunning = False
Else
IsRunning = True
End If
Set applRef = Nothing

End Function
Hope this helps

Djblois
06-04-2009, 11:43 AM
That looks like it is testing to see if Word is open, not if a specific document is open. What if a user has Word open with a different document?

Aussiebear
06-04-2009, 11:55 AM
Create a dummy word file, change the file path name in " mydoc = "C:\Example.docx" line to reflect yours and check, if it determines if the actual file is open.

Kenneth Hobs
06-04-2009, 12:09 PM
If you know how to do it in Excel, then what application are you doing it in here? You can't switch to an Open document if another user has it open. Are you assumming that only you will have it open?

This is a kludge as I assummed several things but it might get you started.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_NORMAL = 1

Sub Test_OpenDoc()
OpenDoc "x:\MSWord\MyFile.doc"
End Sub

Sub OpenDoc(docFilename As String)
Dim docNameOnly As String, rc As Long, caption As String

'Exit if file does not exist
If Dir(docFilename) = "" Then
MsgBox "File does not exist." & vbLf & docFilename, vbCritical, "Error"
Exit Sub
End If

caption = CreateObject("Scripting.FileSystemObject").GetFile(docFilename).Name & _
" - Microsoft Word"
rc = FindWindow(vbNullString, caption)
If rc = 0 Then rc = Shell("winword " & """" & docFilename & """", vbNormalFocus)
ShowWindow rc, SW_NORMAL
End Sub

Djblois
06-04-2009, 12:15 PM
AussieBear you can tell just by looking at the code that it won't test if that specific file is open. The function that does the testing does not refer to the File at all, just the Word Program.

Djblois
06-04-2009, 12:18 PM
Kenneth I mean I know how to test if a Workbook is already open from within Excel. I do not know how to test if a Word document is already open from within Excel.

Emily
06-05-2009, 06:57 AM
Kenneth I mean I know how to test if a Workbook is already open from within Excel. I do not know how to test if a Word document is already open from within Excel.



Function DocOpen(strDocName As String) As Boolean
Dim appWord As Object
Dim wdDoc As Object
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err <> 0 Then GoTo errorhandler

With appWord
Set wdDoc = appWord.Documents(strDocName)
If Err <> 0 Then GoTo errorhandler
End With
DocOpen = True
Exit Function
errorhandler:

End Function
Sub Test()
MsgBox DocOpen("C:\xxxx\Hello.doc")
End Sub

Kenneth Hobs
06-05-2009, 07:29 AM
What may not be obvious from georgiboy's code is that you would need to set a reference to the word object since he used early binding.

Emily used late binding but also used the 2nd parameter of GetObject() which tests for the word application object. The two methods both have good parts. Use the 1st parameter in GetObject() to check for an existing file open in "your" word application.

We can tweak one the two examples using GetOject() if you want to pursue that route.

In my method, I tested and actually opened the file for you. What I wonder though is what's your goal, to (1) test or (2) open or (3) open and set focus with a certain window state.

A more elaborate method that tests for open file and even tells you who has it open can be found at: http://www.xcelfiles.com/IsFileOpenAPI.htm

Djblois
06-11-2009, 11:51 AM
My end result is to test if it is open and if it is open switch to it but if it isn't open to open it.