mack
mack

Reputation: 2965

Set the current new document as active document

When Word opens, it opens a new, unsaved document. I am working on a Word-AddIn and I need to reference the current document, which may be this new unsaved document. I need to set this new document as the active document. How can I do that?

I have searched for the better part of the day today and have had absolutely no luck. If I open an existing document I can set it as active, but opening a document breaks the process for the user.

wordApp = new Microsoft.Office.Interop.Word.Application();
doc = wordApp.Documents.Open(@"C:\Users\user\Desktop\test.docx");               
doc = wordApp.ActiveDocument;

I'm really not sure where to go from here.

Upvotes: 0

Views: 2693

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The Activate method of the Documents class activates the specified document so that it becomes the active document.

wordApp = new Microsoft.Office.Interop.Word.Application();
doc = wordApp.Documents.Open(@"C:\Users\user\Desktop\test.docx");  
doc.Activate();

or if you want to keep a new document as an active one:

wordApp = new Microsoft.Office.Interop.Word.Application();
newDoc = wordApp.ActiveDocument;
doc = wordApp.Documents.Open(@"C:\Users\user\Desktop\test.docx");  
if(newDoc!=null) 
  newDoc.Activate();

Upvotes: 2

Related Questions