October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using Visual Studio to Write Word Add-ins

  • August 4, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Now that the code to submit a blog entry is in place, next you need to notice when the Word document is being saved. The Word object model supports a set of events, and one of them is called DocumentBeforeSave. You can hook into this event, and then execute the necessary code to update the blog whenever the document has been saved.

The logical place to hook into this event is in ThisAddIn.cs' ThisAddIn_Startup method. Here is the code to hook and handle the event:

  private void ThisAddIn_Startup(
    object sender, System.EventArgs e)
  {
    userControl = new SalesToolsUserControl();
    ...
    ApplicationEvents4_DocumentBeforeSaveEventHandler
      handler = new ApplicationEvents4_
        DocumentBeforeSaveEventHandler(
        Application_DocumentBeforeSave);
    this.Application.DocumentBeforeSave += handler;
  }
  
  private void Application_DocumentBeforeSave(
      Microsoft.Office.Interop.Word.Document Doc,
      ref bool SaveAsUI, ref bool Cancel)
  {
    // the document is about to be saved
    if (!SaveAsUI)
    {
      // not saving with a new name
      // or for the first time
      userControl.PublishSaveToBlog(Doc.FullName);
    }
  }

Once the event handler has been assigned to the event, the Application_DocumentBeforeSave method is called each time the user saves the document. Notice how a presentation of the active document is passed to the event handler as the Doc parameter. This object contains a property called FullName, which is used when creating the blog entry text. The code in the user control's PublishSaveToBlog method looks like the following:

  public void PublishSaveToBlog(string filename)
  {
    if (publishToBlogCheckBox.Checked)
    {
      BloggerClient.SubmitBlogPost("Offer updated",
        "The offer \"" + filename +
        "\" has been updated.");
      MessageBox.Show("Blog entry published!");
    }
  }

The end result is a blog entry similar to the one in Figure 9.


Figure 9. A submitted blog entry by the sample application.

Conclusion

In this article, you saw how you can use Visual Studio 2008 to create add-in applications for the Office 2007 suite of products, and more specifically Word 2007. Visual Studio 2008 provides ready-made templates for developing such add- ins, and this gives you a head start. Creating for example a custom task panel is easy as adding a new user control to the project, and then registering it with Word.

The sample application (Figure 10) showed you how you can easily extend Word to transform it to a powerful application platform: you can access SQL databases, call web services, and even associate your code with events in Word. From this perspective, the rule of thumb is that if you can do it from .NET code, you can also do it from an Office add-in.



Click here for larger image

Figure 10. The sample application running inside Word 2007.

If you are looking to build your custom solutions to help businesses run more efficiently, think about whether you could integrate the needed functionality into Office applications like Word, Excel, PowerPoint or Outlook. The possibilities in both business and personal settings are many. And once Visual Studio 2010 and Office 2010 become available, the story will get even better. In the mean time, let's empower the office!

Resource Links

Office Development with Visual Studio
Office Developer Center
Word 2007 Developer Reference
Word Object Model Reference
Google Data APIs Overview

About the Author

Jani Järvinen is a software development trainer and consultant in Finland. He is a Microsoft C# MVP and a frequent author and has published three books about software development. He is the group leader of a Finnish software development expert group at ITpro.fi and a board member of the Finnish Visual Studio Team System User Group. His blog can be found at http://www .saunalahti.fi/janij/. You can send him mail by clicking on his name at the top of the article.





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel