Graviton
Graviton

Reputation: 83326

Create a new Word Document using VSTO

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

Upvotes: 1

Views: 7420

Answers (3)

Luke
Luke

Reputation:

For a VSTO app-level add-in, you can do something like this:

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

where objTemplate can be a template of document

See Documents.Add Method

Upvotes: 2

Jake Ginnivan
Jake Ginnivan

Reputation: 2142

What you are actually after is Office Automation using the PIA's (Primary Interop Assemblies).

VSTO is actually a set of Managed .net extensions which make writing add-ins for Office far easier. For external interaction VSTO is not used at all (though you can still reference VSTO libraries and use some of the helpers if you wanted to).

Have a look at http://support.microsoft.com/kb/316384 to get you started. And google 'word interop create document'

Upvotes: 5

Tim Ridgely
Tim Ridgely

Reputation: 2420

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!

Upvotes: 0

Related Questions