none
How to find out if Word document has been opened? RRS feed

  • Question

  • In my application,  user can download a document from DB to a local drive and open it in Word. But users don't want application to open the same document twice in two different Word windows. For example, user downloads a document from DB to C:\users\...\Documents\MyTestDocument.docx file. If user tries to download this document again, the application should display a message "The MyTestDocument.docx document is already open."

    I wonder, how can I detect if document is already opened in one of the Word instances? In my understanding, I can get all WINWORD processes running on PC using Process[] localByName =Process.GetProcessesByName("WINWORD"). Is it possible to extract underlying objects from the WINWORD process like Application, Document etc.?

    I have to mention that the user's Word application has in-house developed add-in and I can add some code to it if required.

    Maybe, I am exploring in the wrong direction and there are easier ways to find the document in the multiple running Word instances?

    Thank you

    Tuesday, September 19, 2017 3:31 AM

Answers

  • The code I posted tests whether Word is already running and, if so, uses the existing instance. A new instance is only started otherwise. If you want to be able to do something with a document that might already be open, that's a far better approach than automatically starting a new instance. Still, if running multiple instances of Word is what you want to do, you could use something based on:

    Sub TestWordTasks()
    Dim wdApp As New Word.Application, i As Long
    With wdApp
      For i = 1 To .Tasks.Count
        With .Tasks(i)
          If .Name = "Microsoft Word" Then
            'test the task's open documents here
          End If
        End With
      Next
    End With
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by SZBM Thursday, September 21, 2017 4:23 AM
    Wednesday, September 20, 2017 11:14 PM

All replies

  • Your application should:
    • test whether Word is already running and, if so,
    • test whether the current user or another user already has the document open.
    For example, in VBA:

    Sub Demo()
    Application.ScreenUpdating = True
    Dim wdApp As Object, wdDoc As Object, StrDocNm As String
    Dim bStrt As Boolean, bFound As Boolean
    'Check whether the document exists
    StrDocNm = "C:\Users\" & Environ("Username") & "\Documents\Document Name.doc"
    If Dir(StrDocNm) = "" Then
      MsgBox "Cannot find the designated document: " & StrDocNm, vbExclamation
      Exit Sub
    End If
    ' Test whether Word is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start Word, so we can close it later.
    Set wdApp = GetObject(, "Word.Application")
    'Start Word if it isn't running
    If wdApp Is Nothing Then
      Set wdApp = CreateObject("Word.Application")
      If wdApp Is Nothing Then
        MsgBox "Can't start Word.", vbExclamation
        Exit Sub
      End If
      ' Record that we've started Word, so we can terminate it later.
      bStrt = True
    End If
    On Error GoTo 0
    'Check if the document is open.
    bFound = False
    With wdApp
      .Visible = True
      For Each wdDoc In .Documents
        If wdDoc.FullName = StrDocNm Then ' We already have it open
          bFound = True
          Exit For
        End If
      Next
      ' If not open by the current user.
      If bFound = False Then
        ' Check if another user has it open.
        If IsFileLocked(StrDocNm) = True Then
          ' Report and exit if true
          MsgBox "The Word document is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
          If bStrt = True Then .Quit
          Exit Sub
        End If
        ' The file is available, so open it.
        Set wdDoc = .Documents.Open(FileName:=StrDocNm)
        If wdDoc Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrDocNm, vbExclamation
          If bStrt = True Then .Quit
          Exit Sub
        End If
      End If
      With wdDoc
        'Only now can we can process the document!!!
        .Save
        'Close the document if we opened it
        If bFound = False Then .Close
      End With
      If bStrt = True Then .Quit
    End With
    End Sub
    
    Function IsFileLocked(strFileName As String) As Boolean
      On Error Resume Next
      Open strFileName For Binary Access Read Write Lock Read Write As #1
      Close #1
      IsFileLocked = Err.Number
      Err.Clear
    End Function

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, September 19, 2017 5:03 AM
  • Paul, thank you for your comprehensive response. I definitely will apply ideas from your sample in my code. In my understanding, your code detects if document has been opened already in the same WINWORD Windows process. In other words, the code detects if the document has been opened in the same Word instance. In my situation, each document is open in the individual Word instance, i.e.  you can see multiple WINWORD processes running for each document in the Task Manager. My idea was to iterate through Word application documents in each WINWORD process. Hence,  I asked whether Word application object is accessible from outside of process.

    Wednesday, September 20, 2017 9:42 PM
  • The code I posted tests whether Word is already running and, if so, uses the existing instance. A new instance is only started otherwise. If you want to be able to do something with a document that might already be open, that's a far better approach than automatically starting a new instance. Still, if running multiple instances of Word is what you want to do, you could use something based on:

    Sub TestWordTasks()
    Dim wdApp As New Word.Application, i As Long
    With wdApp
      For i = 1 To .Tasks.Count
        With .Tasks(i)
          If .Name = "Microsoft Word" Then
            'test the task's open documents here
          End If
        End With
      Next
    End With
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by SZBM Thursday, September 21, 2017 4:23 AM
    Wednesday, September 20, 2017 11:14 PM