Question

I'm trying to automate a tedious process that currently involves launching Word, Creating a new document from a .dot, saving it, running one or two plug-ins that were written in C# using VSTO, saving it again, exiting the document, and exiting Word.

I want to write a C# commandline app that the user can launch with one or two args (conveying all the information that would normally require interaction with dialogs in Word), then walk away from as it runs without further interaction... suppressing any and all focus-stealing by Word while it's running if necessary and possible.

Is there some straightforward way to accomplish this? Here's a Java-like pseudocode example of what I have in mind:

// magic to non-interactively launch Word and expose it as an object
WordHost word = xx; 

// create new Word document based on a specific template that isn't the default one.
WordDocument doc = MSWord.create("z:\path\to\arbitraryTemplate.dot");

// If we can avoid physically saving it at this point and just assign a concrete
// file path, it would be even better because the network is glacially slow.
doc.saveAs("z:\path\to\newDoc.docx");

// someZeroArgPlugin and aTwoArgPlugin are VSTO plugins written with C#
doc.someZeroArgPlugin(); 
doc.aTwoArgPlugin("first", "second");

// done!
doc.save();
doc=null;
word=null; // something like word.unload() first?
// now do more things that don't involve Word directly...

Assuming I'm on the right track...

  • I'm pretty sure I can find most of what I need to know by searching... once I figure out what I need to be searching for. What should I be searching for?

  • What kind of project do I want to be creating in Visual Studio? A .net 4.5 C# console application? A Word 2010 Add-In? Some other kind of project?

Details that might or might not make a difference:

  • my program will only be run on computers that have Word 2010 installed. Compatibility with older versions isn't necessary.

  • It would be nice if it can run under Vista, but it only has to work under Win7.

  • I have Visual Studio Ultimate 2012

Was it helpful?

Solution

Here's what you'll need to do:

  1. Have Visual Studio and Office installed.
  2. Create a C# console project using the .NET framework of your choice (recommend 4.0 or above).
  3. Add a reference to the Word COM library (Project menu => Add Reference, COM tab, Microsoft Word XX.0 Object library -- Word 2010 is 14.0).
  4. Set the Embed Interop Types setting to false for the references added above
    1. Expand References in Solution Explorer
    2. Select Microsoft.Office.Core, Microsoft.Office.Interop.Word and VBIDE
    3. Right-click and select Properties to bring up the Properties panel for the references.
    4. In the Properties panel, set Embed Interop Types to False
  5. Code away.

Here's some sample code.

using System;
using Microsoft.Office.Interop.Word;

namespace CSharpConsole
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            var application = new ApplicationClass();
            var document = application.Documents.Add();
            document.SaveAs("D:\test.docx");
            application.Quit();
        }
    }
}

For more information, see http://msdn.microsoft.com/en-us/library/office/ff601860(v=office.14).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top