Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Utility to generate Word documents from templates using Visual Studio 2010 and Open Xml 2.0 SDK - Part 3

, 12 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is the third post of this series.

Download Source Code

This is the third post of this series. The earlier posts can be read at Part 1 and Part 2.

In Part 1 I discussed about

  • Document generation using Content Controls and Open XML 2.0 SDK
  • Creating Word templates
  • Implementation and Samples

In Part 2 I discussed about

  • List of functionalities that can be achieved using the utility/source code
  • Description regarding Samples provided with utility
  • New samples added in this update

In this part I’ll discuss about the sample that shows one of the ways to “Refresh the document from within the Word(e.g. right click on document and click Refresh) using document-level customizations for Word 2007 and Word 2010“. On click of Refresh Document the content of the document is refreshed as displayed below

image

Project “WordDocumentGenerator.WordRefreshableDocumentAddin” has been added to the utility for this sample. The steps followed for creating this sample are listed below.

  1. Added a new Word 2010 Document project as displayed below
  2. image

  3. Updated the document by adding the content controls as displayed below:
  4. image

  5. Added a new Command bar button i.e. “Refresh Data” to the Command bar. On click of this button the document will be refreshed. The common scenario will be to refresh data from the Service. “WordDocumentGenerator.WordRefreshableDocumentAddin” is the document level customization project. This project references “WordDocumentGenerator.Library” and “WordDocumentGenerator.Client” projects.
  6. The code snippet to add a new Command bar button is:

/// <span class="code-SummaryComment"><summary>
</span>/// Adds the command bar.
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="cmdBr">The CMD br.</param>
</span>/// <span class="code-SummaryComment"><param name="handler">The handler.</param>
</span>/// <span class="code-SummaryComment"><param name="index">The index.</param>
</span>/// <span class="code-SummaryComment"><param name="tag">The tag.</param>
</span>/// <span class="code-SummaryComment"><param name="caption">The caption.</param>
</span>/// <span class="code-SummaryComment"><returns></returns>
</span>private CommandBarButton AddCommandBar(CommandBar cmdBr, 
   _CommandBarButtonEvents_ClickEventHandler handler, 
   int index, string tag, string caption)
{
    CommandBarButton cmdBtn = (CommandBarButton)cmdBr.FindControl(
      MsoControlType.msoControlButton, 0, tag, missing, missing);

    if ((cmdBtn != null))
    {
        cmdBtn.Delete(true);
    }

    cmdBtn = (CommandBarButton)cmdBr.Controls.Add(
      MsoControlType.msoControlButton, missing, missing, index, true);
    cmdBtn.Style = MsoButtonStyle.msoButtonCaption;
    cmdBtn.Caption = caption;
    cmdBtn.Tag = tag;
    cmdBtn.Visible = true;

    cmdBtn.Click -= handler;
    cmdBtn.Click += handler;

    if (!commandBarsTags.Contains(tag))
    {
        commandBarsTags.Add(tag);
    }
    return cmdBtn;
}

On click of refresh data the main steps are:

  1. Get package steam from the document
  2. Microsoft.Office.Interop.Word.Document doc = app.ActiveDocument;
     
    // Get the active documents as stream of bytes
    byte[] input = doc.GetPackageStream(); 
  3. Call the Service/Client method that generates/refreshes the document. This can be a Server or a direct call. In this method it’s a direct call.
  4. // Generate document on the Server.
    // AddInService can be a proxy to service, however here it's direct call
    byte[] output = AddInService.GenerateDocument(input);
    /// <span class="code-SummaryComment"><summary>
    </span>/// Generates the document.
    /// <span class="code-SummaryComment"></summary>
    </span>/// <span class="code-SummaryComment"><param name="documentStream">The document stream.</param>
    </span>/// <span class="code-SummaryComment"><returns></returns>
    </span>public static byte[] GenerateDocument(byte[] documentStream)
  5. Update the document contents
  6. XDocument xDoc = OPCHelper.OpcToFlatOpc(wordDocument.Package);
    string openxml = xDoc.ToString();                        
    doc.Range().InsertXML(openxml);

These are partial code snippets to show the code flow. For complete sample please download the source code.

References:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

atverma
Software Developer (Senior)
India India
Atul works at Microsoft as a .NET consultant. As a consultant his job is to design, develop and deploy enterprise level secure and scalable solutions using Microsoft Technologies.
 
His technical expertise include .NET Framework(4.0, 3.5, 3.0), WPF, WCF, SharePoint 2010, ASP.net, AJAX, Web Services, Enterprise Applications, SQL Server 2008, Open Xml, MS Word Automation.
 
Follow him on twitter @verma_atul
 
He blogs at
http://www.atulverma.com
http://blogs.msdn.com/b/atverma
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 12 Jan 2012
Article Copyright 2012 by atverma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid