0
votes

I've developed a VSTO Add-In. I've created a Ribbon, which makes a search button appearing into MS Word's Add-In tab.

This Search button opens a Window which allows users to Search the Document from Cloud and Open/ Download the Same.

I want to open the document being searched and downloaded from the cloud to be opened into existing MS Word Window.

User Interface Image

My Logic is: When selected document is downloaded by user. I'm copying it in the Local Directory and trying to open the same.

Sample Code:

public void OpenDocument(string documentPath, Form form)  
{

_searchForm.Close();
_searchForm.Dispose();


Word.Document newDoc = Globals.ThisAddIn.Application.Documents.Open(documentPath);
newDoc.Activate();

}

But, I'm getting following exception.

You cannot close Microsoft Word because a dialog box is open. Click OK, switch to Word, and then close the dialog box.

1
Well, if OpenDocument is being called from the form, then the form cannot finish/be disposed at this point in time because it's still executing. Also, the error message mentions trying to close the Word application. I don't see any code in the question that relates to that, so exactly where the problem is is unclear...Cindy Meister
I've been able to solve the exception i.e. 'You cannot close Microsoft Word because a dialog box is open. Click OK, switch to Word, and then close the dialog box'. by using the code below: Word.Document doc = this.Application.Documents.Add(path, System.Type.Missing, System.Type.Missing); but it's opening a new MS Word Window. I want to open it in existing (Active Word Window)Ashish Tripathi
By design, for over a decade, it's not possible to open more than one document / Word window. Every document is forced to open in a separate window, even if all these windows are part of the same Word.Application instance. Is this what you're referring to?Cindy Meister
Thanks Sindy. Is there any way, I can close one MS Word Window, when I'm adding new document (which's opening in a separate window)? Your help is highly appreciated.Ashish Tripathi
Probably, but without exact details it's not possible to say for sure.Cindy Meister

1 Answers

1
votes

Opening the document in active window of MS Word is not possible. But, I could manage to use a work around to achieve the same.

When any new document is being opened through MS Word, I'm checking, if the parent window (Active Window) is blank, then

  1. I'm adding the new document to be opened

  2. Then, I'm closing the active window (after checking, if it's blank).

Sample Code

//Get the active document.
Word.Document activeDocument = Globals.ThisAddIn.Application.ActiveDocument;

//Check, if Active Window is blank, then we can close it. Once the new Window is Opened. If not, 
//we'll open the document in new window
bool closeParentWindow = activeDocument.Content.End - activeDocument.Content.Start == 1 ;


string activeDocName = activeDocument.Name;

//Add the new document which's supposed to open
Word.Document newDoc = this.Application.Documents.Add(path, System.Type.Missing, System.Type.Missing);
newDoc.ActiveWindow.Caption = matterDocument.Document.Name;


if (closeParentWindow)
{
    //If Earlier Window/ Document Could be closed. Then need to invoke Close
    Word._Document docToClose = Application.Documents[activeDocName] as Word._Document;
    docToClose.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
}