This is the third post of this series. The earlier posts can be read at Part 1 and Part 2. The code is available for download at Utility Source Code.
In Part 1 I discussed about
In Part 2 I discussed about
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
Project “WordDocumentGenerator.WordRefreshableDocumentAddin” has been added to the utility for this sample. The steps followed for creating this sample are listed below
The code snippet to add a new Command bar button is
/// <summary> /// Adds the command bar. /// </summary> /// <param name="cmdBr">The CMD br.</param> /// <param name="handler">The handler.</param> /// <param name="index">The index.</param> /// <param name="tag">The tag.</param> /// <param name="caption">The caption.</param> /// <returns></returns> 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; }
/// <summary>
/// Adds the command bar.
/// </summary>
/// <param name="cmdBr">The CMD br.</param>
/// <param name="handler">The handler.</param>
/// <param name="index">The index.</param>
/// <param name="tag">The tag.</param>
/// <param name="caption">The caption.</param>
/// <returns></returns>
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 Microsoft.Office.Interop.Word.Document doc = app.ActiveDocument; // Get the active documents as stream of bytes byte[] input = doc.GetPackageStream(); 2. 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. // Generate document on the Server. AddInService can be a proxy to service, however here it's direct call byte[] output = AddInService.GenerateDocument(input);
On click of refresh data the main steps are
1. Get package steam from the document
Microsoft.Office.Interop.Word.Document doc = app.ActiveDocument;
// Get the active documents as stream of bytes
byte[] input = doc.GetPackageStream();
2. 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.
// Generate document on the Server. AddInService can be a proxy to service, however here it's direct call
byte[] output = AddInService.GenerateDocument(input);
/// <summary> /// Generates the document. /// </summary> /// <param name="documentStream">The document stream.</param> /// <returns></returns> public static byte[] GenerateDocument(byte[] documentStream) {} 3. Update the document contents XDocument xDoc = OPCHelper.OpcToFlatOpc(wordDocument.Package); string openxml = xDoc.ToString(); doc.Range().InsertXML(openxml);
/// Generates the document.
/// <param name="documentStream">The document stream.</param>
public static byte[] GenerateDocument(byte[] documentStream)
{}
3. Update the document contents
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: