Results 1 to 5 of 5

Thread: How to determine an already open word document? **RESOLVED**

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    Thumbs up How to determine an already open word document? **RESOLVED**

    I am well stuck,

    How do you determine if a word document is already open within VB.NET?

    I would be grateful of any help.

    Thanks.
    Last edited by azagthoth; May 7th, 2004 at 07:06 AM.

  2. #2
    Addicted Member
    Join Date
    Dec 2002
    Posts
    175
    Is it Word instance or the actual document?

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Do you want to check only based on the file name?
    The following dll and the code might help you, but it is not a good way, because you might have two files with same name but form different locations and you can't say which is open using this method.
    VB Code:
    1. Dim main As Win32Util.Win32Window
    2.         Dim id As IntPtr
    3.         Dim proc() As Process = Process.GetProcessesByName("winword")
    4.         MessageBox.Show(proc.Length)
    5.         If proc.Length > 0 Then
    6.             id = proc(0).Handle
    7.             main = New Win32Util.Win32Window(id)
    8.             If main.FindWindow(Nothing, "My file - Microsoft Word").IsNull Then
    9.                 MessageBox.Show("Not Found!")
    10.             Else
    11.                 MessageBox.Show("Found!")
    12.             End If
    13.         Else
    14.             MessageBox.Show("Winword is not running at all!")
    15. End If

    You may also try opening the document in word and if it is open it will through an error, but if not, it will open and I guess that's not what you desire.
    Attached Files Attached Files
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    Thanks for your replies.

    What I am trying to do is to check to see if the user has a word document open. I am writing a report out using a word template (located on the local disk) and subsequently saving the document (to a network area) using variables that are unique to the customer.

    Firstly, I need to check to see if the word template is not open. If the template is open then I will produce an error message telling the user that they must close the template before proceeding and exit out of subroutine with no further processing.

    Secondly, I need to check to see if the file I am saving is not open. I'm not bothered if the file exists as the users of the system are quite happy to have the file overwritten, but the file cannot be exclusively assigned to another user.

    I am currently having a look at checking for temporary word files open in the directory to ascertain whether a certain document is open or not. However, I will try the dll posted. Thanks once again for your help.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    Lightbulb

    Got it.

    When a word document is opened, a temporary file is created with a prefix of ~$. However, if the file name is over a certain length (4 or 5 chars I think). In order to get around this problem, I prefix the file that I am saving with 2 underscores (i.e. __MyWordDocument.doc).

    When __MyWordDocument.doc is open, it's temporary file ~$MyWordDocument.doc exists in the same directory. In order to check that it exists, I wrote the following code in my subroutine

    Code:
                If File.Exists(Application.StartupPath & "\~$MyWordDocument.doc") Then
    
                    MsgBox("File already being edited.  Please close this file before trying again")
    
                    Exit Sub
    
                End If
    This stops any attempts to open an already open word document.

    Thanks once again for your help.

Posting Permissions

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



Click Here to Expand Forum to Full Width