views:

1273

answers:

2

How can I create a new Word document pro grammatically using Visual Studio Tools for Office?

A: 

Hello, you can Try something like this:

Globals.ThisAddIn.Application.Documents.Add(ref objTemplate, ref missingType, ref missingType, ref missingType); 

where objTemplate can be a template of document

Hope this Help

+1  A: 

Now, I might be wrong on this, but I don't believe you can actually make a new Word doc using VSTO. I'm not intimately familiar with VSTO, so forgive me if I'm incorrect on that point.

I do know that you can use Office Interop libraries to do this, however.

To download the libraries, just do a search for "office interop assemblies," possibly including the Office version you want (ex: "office interop assemblies 2007").

Once you've included the Word Interop assembly into your application (using Add Reference), you can do something like:

using Word = Microsoft.Office.Interop.Word;

object missing = System.Reflection.Missing.Value;
Word.Application app = new Word.ApplicationClass();
Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc.Activate();
app.Selection.TypeText("This is some text in my new Word document.");
app.Selection.TypeParagraph();

Hope that helps!

Tim Ridgely