none
"File Is Locked" Dialog not being focused when using the Documents.Open() function RRS feed

  • Question

  • Hello All,

    When using the Documents.Open() method in VB, if the file is not opened by another user, then the document opens perfectly. However, if in use, the "File In Use" dialog box will pop up and will not be in focus. This means that users have to Alt+Tab to the window to proceed; until this is done, the application locks itself up. Has anyone else faced this problem before, and if so, were they able to resolve it? Thanks very much for your assistance.

    • Edited by Abishai7 Tuesday, April 30, 2013 8:23 PM
    • Moved by Youen Zen Thursday, May 2, 2013 8:03 AM From Visual Basic
    Tuesday, April 30, 2013 8:19 PM

Answers

  • However, if in use, the "File In Use" dialog box will pop up and will not be in focus. 

    Hello,

    I create a Windows Form application to reproduce this issue. (A simple dialog, with a button which will open a specific document.) If I run this application in Visual Studio, press the button to open a document which is already opened, the "File In Use" dialog will show in focus. But if I run the exe file directly to do the same thing, the "File In Use" dialog will show but won't be in focus. (Visual Studio 2012, Office 2013, Windows 8)

    Based on my understanding, this is a system-level issue. There may be some implicit action making the prompt dialog losing focus.

    As a workaround, you can check whether the document you want to open is already opened:

    Dim WordApp As Word.Application
    WordApp = Marshal.GetActiveObject("Word.Application")
    
    If WordApp Is Nothing Then
        'run Word application and open the document
    End If
    
    For Each doc As Word.Document In WordApp.Documents
        If doc.Name = "Sample.docx" Then
            'The document is already opened
        End If
    Next
    Thanks,

    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 2, 2013 12:37 PM
    Moderator

All replies

  • However, if in use, the "File In Use" dialog box will pop up and will not be in focus. 

    Hello,

    I create a Windows Form application to reproduce this issue. (A simple dialog, with a button which will open a specific document.) If I run this application in Visual Studio, press the button to open a document which is already opened, the "File In Use" dialog will show in focus. But if I run the exe file directly to do the same thing, the "File In Use" dialog will show but won't be in focus. (Visual Studio 2012, Office 2013, Windows 8)

    Based on my understanding, this is a system-level issue. There may be some implicit action making the prompt dialog losing focus.

    As a workaround, you can check whether the document you want to open is already opened:

    Dim WordApp As Word.Application
    WordApp = Marshal.GetActiveObject("Word.Application")
    
    If WordApp Is Nothing Then
        'run Word application and open the document
    End If
    
    For Each doc As Word.Document In WordApp.Documents
        If doc.Name = "Sample.docx" Then
            'The document is already opened
        End If
    Next
    Thanks,

    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 2, 2013 12:37 PM
    Moderator
  • Hi Abishai

    I agree with Damon that this problem is at the system level. Word (as does most applications) puts a file lock on files that are already open. It would probably make most sense for your application to first check whether the file is locked. Unfortunately, I'm not able to tell you what code that is, but a forum the specializes in the .NET Framework base classes or Network classes (as this must be in a network environment) probably could.

    As for seeing the alert, you might try first activating the Word.Application instance and see if that helps at all.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, May 3, 2013 8:13 AM
    Moderator
  • Excellent, thank you very much for the confirmation. I will make sure to use this workaround for the time being. Thank you for the prompt reply.
    Monday, May 6, 2013 3:29 PM