Consulting

Results 1 to 10 of 10

Thread: How do I check if a word file is open from code?

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    How do I check if a word file is open from code?

    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.
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Maybe something like this...

    You will need to set the word reference

    [vba]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[/vba]
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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?
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    [VBA]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[/VBA]

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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.
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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.
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  8. #8
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Quote Originally Posted by Djblois
    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.
    [vba]

    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

    [/vba]

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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.
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •