Microsoft Word - find open dialogs

Charles Epoc Waudby 1 Reputation point
2023-06-22T09:02:10.28+00:00

If a dialog is open in Word then certain methods will fail, example below. We can get round this if there is a document open wrapping the call into an error handler but if no documents are open this cannot be done (I don't want to randomly open a temporary new document to test because if a dialog is open we cannot close the new document it for the same reason).

Microsoft.Office.Interop.Word.WdProtectionType ProtectionType = document.ProtectionType;

This is a problem because we do not want to open a document if we cannot control things like ProtectionType (these are confidential clinical documents so control is critical).

Dialogs are a window (not a Microsoft.Office.Interop.Word.Window) and Dialog objects do not have a "state" and there does not appear to be a collection of open dialogs.

The parent handle of a Dialog appears to be Desktop so iterating through all windows and checking the title can't work because Dialogs share the same title in some cases as other applcation's windows, eg "File Save".

After weeks of research and testing and posting some other forums I am fairly sure that there really is no way to check for open Dialogs unless there is an existing document open in Word.

Does anyone know how to check for open Dialogs or am I correct and it is impossible?

Regards,

Charles

Developer technologies | Windows Forms
Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2023-06-22T10:11:06.9633333+00:00

    Maybe like this:

    [DllImport( "user32" )]
    static extern bool IsWindowEnabled( IntPtr hWnd );
    
    . . .
    
    bool is_dialog_opened = Process.GetProcessesByName( "WINWORD" ).Any( p => !IsWindowEnabled( p.MainWindowHandle ) );
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.