<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Brian Jones: Office Extensibility</title><subtitle type="html">I'm Brian Jones, a program manager in Office. I've been working on the XML functionality and file formats in Office, as well as other technologies folks use for extending Office in various ways. In this blog, I'll focus on the types of Business Applications people are building that leverages Office functionality. </subtitle><id>http://blogs.msdn.com/brian_jones/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/brian_jones/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2009-02-13T11:16:00Z</updated><entry><title>Embedding an Open XML File in another Open XML File</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/06/30/embedding-an-open-xml-file-in-another-open-xml-file.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/06/30/embedding-an-open-xml-file-in-another-open-xml-file.aspx</id><published>2009-06-30T22:26:00Z</published><updated>2009-06-30T22:26:00Z</updated><content type="html">&lt;P&gt;A couple of weeks ago I gave a presentation on the Open XML SDK to a few customers, where I was asked questions on how to embed files within Open XML documents. I thought it would be a good opportunity to devote a couple of posts around this topic. In today's post I am going to show you how to embed an Open XML file in another Open XML file. Specifically, I am going to show you how to embed an Excel spreadsheet (.xlsx) into a Word document (.docx). Next post will cover how to embed other file types in Open XML files. &lt;/P&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/EmbedExcelFileIntoWord.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/EmbedExcelFileIntoWord.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;
&lt;OBJECT width=425 height=344&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/xunFeqCW2GA&amp;amp;hl=en&amp;amp;fs=1"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
                                    
            &lt;embed src="http://www.youtube.com/v/xunFeqCW2GA&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash"
                allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To embed an Excel spreadsheet into a Word document we can take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a template in Word that contains a content control that will be used to demarcate the region where the embedded object will be inserted &lt;/LI&gt;
&lt;LI&gt;Open up the Word document via the Open XML SDK and access its main document part &lt;/LI&gt;
&lt;LI&gt;Add an image part to the document (this image will be a placeholder image of the embedded object file) &lt;/LI&gt;
&lt;LI&gt;Add an embedded package part to the document &lt;/LI&gt;
&lt;LI&gt;Create a paragraph that contains the embedded object &lt;/LI&gt;
&lt;LI&gt;Locate the content control that will contain the embedded object &lt;/LI&gt;
&lt;LI&gt;Swap out the content control for the newly created paragraph &lt;/LI&gt;
&lt;LI&gt;Save changes made to the Word document &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Note that the steps outlined above are just one method to accomplish this scenario. &lt;/P&gt;
&lt;P&gt;For the sake of this example, let's say I am starting with the following Word document: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan1.png" mce_src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan1.png"&gt; &lt;/P&gt;
&lt;P&gt;This document contains a content control, named "EmbedObject," which will contain my embedded object. In addition, let's say I have the following Excel spreadsheet I wish to embed: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan2.png" mce_src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan2.png"&gt; &lt;/P&gt;
&lt;H2&gt;Embedded Objects in Open XML &lt;/H2&gt;
&lt;P&gt;Before we get into the code, I wanted to talk more about embedded objects. Office has three ways of storing embedded objects: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Those where Office persists the &lt;A href="http://msdn.microsoft.com/en-us/library/aa380015.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa380015.aspx"&gt;IStorage&lt;/A&gt; as given to Office during OLE operations &lt;/LI&gt;
&lt;LI&gt;Those where Office persists the IStorage as given during OLE operations, but gives the embedded object a friendly extension and filename. This method assumes that the embedded object is a native file format of the application in question &lt;/LI&gt;
&lt;LI&gt;Those where Office interprets the IStorage given during OLE operations as simply a wrapper for a package and only stores the package. This method assumes that the package conforms to Open Packaging Conventions &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The major difference between #0 vs. #1 and #2 is in how objects are embedded within a file. Types #1 and #2 allows developers working with Open XML files to more easily extract and insert embedded objects because there is no need to talk to an OLE server. Instead, developers can simply read/write embedded objects as if they were reading from or writing to files on disk. Office differentiates between these three types by looking for a specific registry key under HKCR\CLSID\{Apps_OLE_Storage_CLSID}, where Apps_OLE_Storage_CLSID is the CLSID of the OLE storage server. The Office applications look for a subkey named IPersistStorageType and determines the type of the embedded object in the following manner: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Office assumes the embedded object is type #0 if no subkey is specified or if the value of the subkey is 0 &lt;/LI&gt;
&lt;LI&gt;Office assumes the embedded object is type #1 if the subkey has a value of 1 &lt;/LI&gt;
&lt;LI&gt;Office assumes the embedded object is type #2 if the subkey has a value of 2 &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The cool thing is that other applications can take advantage of this reg key. For example, if an application writes out a value of 1 for this subkey for a particular file format then the Office applications will embed files of that type natively in the Open XML file formats. &lt;/P&gt;
&lt;P&gt;One more thing to note is that all embedded object types require a prog id, which you can find from the registry, as well as an image representation of the object. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As mentioned above, when an object is embedded in a document, both a visual representation of the object and the underlying data is stored. The visual representation is simply an image of what you would see if you were to activate the object. For the sake of this solution, my visual representation of the document will be a placeholder image that indicates to users how to refresh the embedded object and will look like the following image: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan3.png" mce_src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan3.png"&gt; &lt;/P&gt;
&lt;P&gt;Looking at the steps outlined above in the &lt;A href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=4689&amp;amp;postid=9810106#_Solution" mce_href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=4689&amp;amp;postid=9810106#_Solution"&gt;Solution&lt;/A&gt; section, here is the code snippet to accomplish steps two through four: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE style="BORDER-COLLAPSE: collapse; BACKGROUND: #dbe5f1" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 650px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD style="BORDER-BOTTOM: black 0.5pt solid; BORDER-LEFT: black 0.5pt solid; PADDING-BOTTOM: 1px; PADDING-LEFT: 7px; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; BORDER-RIGHT: black 0.5pt solid; PADDING-TOP: 1px" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(output, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ImagePart&lt;/SPAN&gt; imagePart = mainPart.AddImagePart(&lt;SPAN style="COLOR: #2b91af"&gt;ImagePartType&lt;/SPAN&gt;.Png); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;imagePart.FeedData(&lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.Open(&lt;SPAN style="COLOR: #a31515"&gt;"placeholder.png"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;FileMode&lt;/SPAN&gt;.Open)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;EmbeddedPackagePart&lt;/SPAN&gt; embeddedObjectPart = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;mainPart.AddEmbeddedPackagePart(&lt;SPAN style="COLOR: #a31515"&gt;@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;embeddedObjectPart.FeedData(&lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.Open(&lt;SPAN style="COLOR: #a31515"&gt;"embed.xlsx"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;FileMode&lt;/SPAN&gt;.Open)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The placeholder.png refers to the placeholder image I showed you above and the embed.xlsx file is the spreadsheet that will be embedded. The string "&lt;SPAN style="FONT-FAMILY: Courier New; COLOR: #a31515; FONT-SIZE: 10pt"&gt;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&lt;/SPAN&gt;" represents the content type of an Excel spreadsheet with an extension .xlsx. You can find the content type of a particular file by going to HKCR\.XXX, where XXX is the extension of the file format, and looking for a sub key named "Content Type." &lt;/P&gt;
&lt;P&gt;The next step is to create a paragraph that represents our embedded object. Like my other post on &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspx"&gt;importing SmartArt from PowerPoint to Word&lt;/A&gt;, I am going to take advantage of the Document Reflector tool that ships free with the SDK. Using this tool's output as a starting point, I am able to generate the necessary paragraph with the following code snippet: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE style="BORDER-COLLAPSE: collapse; BACKGROUND: #dbe5f1" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 820px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD style="BORDER-BOTTOM: black 0.5pt solid; BORDER-LEFT: black 0.5pt solid; PADDING-BOTTOM: 1px; PADDING-LEFT: 7px; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; BORDER-RIGHT: black 0.5pt solid; PADDING-TOP: 1px" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; CreateEmbeddedObjectParagraph(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; imageId, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; embedId) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EmbeddedObject&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Shapetype&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 216pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Stroke&lt;/SPAN&gt;() { JoinStyle = V.&lt;SPAN style="COLOR: #2b91af"&gt;StrokeJoinStyleValues&lt;/SPAN&gt;.Miter }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 216pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formulas&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"if lineDrawn pixelLineWidth 0"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"sum @0 1 0"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"sum 0 0 @1"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @2 1 2"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @3 21600 pixelWidth"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @3 21600 pixelHeight"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"sum @0 0 1"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @6 1 2"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @7 21600 pixelWidth"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"sum @8 21600 0"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"prod @7 21600 pixelHeight"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 252pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Formula&lt;/SPAN&gt;() { Equation = &lt;SPAN style="COLOR: #a31515"&gt;"sum @10 21600 0"&lt;/SPAN&gt; }), &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 216pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Path&lt;/SPAN&gt;() { AllowGradientShape = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.T, ConnectionPointType = OVML.&lt;SPAN style="COLOR: #2b91af"&gt;ConnectValues&lt;/SPAN&gt;.Rectangle, AllowExtrusion = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.F }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 216pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; OVML.&lt;SPAN style="COLOR: #2b91af"&gt;Lock&lt;/SPAN&gt;() { Extension = V.&lt;SPAN style="COLOR: #2b91af"&gt;ExtensionHandlingBehaviorValues&lt;/SPAN&gt;.Edit, AspectRatio = OVML.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.T } &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;) { Id = &lt;SPAN style="COLOR: #a31515"&gt;"_x0000_t75"&lt;/SPAN&gt;, CoordinateSize = &lt;SPAN style="COLOR: #a31515"&gt;"21600,21600"&lt;/SPAN&gt;, Filled = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.F, Stroked = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.F, OptionalNumber = 75, PreferRelative = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanValues&lt;/SPAN&gt;.T, EdgePath = &lt;SPAN style="COLOR: #a31515"&gt;"m@4@5l@4@11@9@11@9@5xe"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;Shape&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; V.&lt;SPAN style="COLOR: #2b91af"&gt;ImageData&lt;/SPAN&gt;() { Title = &lt;SPAN style="COLOR: #a31515"&gt;""&lt;/SPAN&gt;, RelationshipId = imageId } &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;) { Id = &lt;SPAN style="COLOR: #a31515"&gt;"_x0000_i1025"&lt;/SPAN&gt;, Style = &lt;SPAN style="COLOR: #a31515"&gt;"width:500pt;height:400pt"&lt;/SPAN&gt;, Ole = V.&lt;SPAN style="COLOR: #2b91af"&gt;BooleanEntryWithBlankValues&lt;/SPAN&gt;.Empty, Type = &lt;SPAN style="COLOR: #a31515"&gt;"#_x0000_t75"&lt;/SPAN&gt; }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; OVML.&lt;SPAN style="COLOR: #2b91af"&gt;OleObject&lt;/SPAN&gt;() { Type = OVML.&lt;SPAN style="COLOR: #2b91af"&gt;OLEValues&lt;/SPAN&gt;.Embed, ProgId = &lt;SPAN style="COLOR: #a31515"&gt;"Excel.Sheet.12"&lt;/SPAN&gt;, ShapeId = &lt;SPAN style="COLOR: #a31515"&gt;"_x0000_i1025"&lt;/SPAN&gt;, DrawAspect = OVML.&lt;SPAN style="COLOR: #2b91af"&gt;OLEDrawAspectValues&lt;/SPAN&gt;.Content, ObjectId = &lt;SPAN style="COLOR: #a31515"&gt;"_1307530183"&lt;/SPAN&gt;, Id = embedId } &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;) { DxaOriginal = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)10957U, DyaOriginal = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)8455U }) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; p; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The last step of the solution is to swap out the content control for this newly created paragraph. This code is very similar to a lot of my previous posts where I used content controls as semantic structures. Here is the code snippet to accomplish this task: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE style="BORDER-COLLAPSE: collapse; BACKGROUND: #dbe5f1" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD style="BORDER-BOTTOM: black 0.5pt solid; BORDER-LEFT: black 0.5pt solid; PADDING-BOTTOM: 1px; PADDING-LEFT: 7px; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; BORDER-RIGHT: black 0.5pt solid; PADDING-TOP: 1px" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = CreateEmbeddedObjectParagraph(mainPart.GetIdOfPart(imagePart), &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;mainPart.GetIdOfPart(embeddedObjectPart)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;.Where(s =&amp;gt; s.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtProperties&lt;/SPAN&gt;&amp;gt;().GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;.Equals(&lt;SPAN style="COLOR: #a31515"&gt;"EmbedObject"&lt;/SPAN&gt;)).First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;OpenXmlElement&lt;/SPAN&gt; parent = sdt.Parent; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;parent.InsertAfter(p, sdt); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;sdt.Remove(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-FAMILY: Courier New; FONT-SIZE: 10pt"&gt;mainPart.Document.Save();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Running this code I should end up with a document that looks like the following: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan4.png" mce_src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan4.png"&gt; &lt;/P&gt;
&lt;P&gt;Upon activating the embedded object I will see the following: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan5.png" mce_src="http://zeyadrajabi.members.winisp.net/063009_2157_Embeddingan5.png"&gt; &lt;/P&gt;
&lt;P&gt;Pretty easy stuff! Next time I will show you how to embed other file formats, like PDF. &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9810106" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Removing Page and Section Breaks from a Word Document</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/06/15/removing-page-and-section-breaks-from-a-word-document.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/06/15/removing-page-and-section-breaks-from-a-word-document.aspx</id><published>2009-06-15T14:00:00Z</published><updated>2009-06-15T14:00:00Z</updated><content type="html">&lt;P&gt;In today's post I am going to show you how to remove page and section breaks within a Word document using the Open XML SDK. Removing these two types of breaks is similar, but requires two different approaches. Let's start off by jumping into removing page breaks. &lt;/P&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/RemovePageBreaks.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/RemovePageBreaks.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H1&gt;Solution to Remove Page Breaks &lt;/H1&gt;
&lt;P&gt;To remove page breaks in a document we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open the Word document via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Get access to the main document part &lt;/LI&gt;
&lt;LI&gt;Find all page breaks within the main document part &lt;/LI&gt;
&lt;LI&gt;For every page break found, remove it from the document &lt;/LI&gt;
&lt;LI&gt;Save changes &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;For the sake of this example, let's say I am starting with the following Word document: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag1.png" mce_src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag1.png"&gt; &lt;/P&gt;
&lt;P&gt;This document has a page break (shown outlined in red) on the first page. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;The code is pretty straight forward and follows the solution steps as described above in the solutions section: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemovePageBreaks(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(filename, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Break&lt;/SPAN&gt;&amp;gt; breaks = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Break&lt;/SPAN&gt;&amp;gt;().ToList(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Break&lt;/SPAN&gt; b &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; breaks) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;b.Remove(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;mainPart.Document.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Pretty easy stuff! &lt;/P&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Running this code I should end up with a document that looks like the following: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag2.png" mce_src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag2.png"&gt; &lt;/P&gt;
&lt;P&gt;Now let's see how to remove section breaks within a document. Before I actually jump into the solution of removing sections, I want to talk a bit about section breaks within a Word document. &lt;/P&gt;
&lt;H1&gt;Section Breaks in a Word Document &lt;/H1&gt;
&lt;P&gt;WordprocessingML does not natively store the concept of pages, since it is based on paragraphs and runs. Instead it uses sections to specify groups of paragraphs that have a specific set of page properties. &lt;/P&gt;
&lt;P&gt;Every Word document has at least one section, where each section specifies page properties (like page size, orientation, margins, etc), header/footer references, column information, etc. Given this information, there are really two high level types of sections: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;A section as a paragraph property – A document may have zero or more of these types of sections &lt;/LI&gt;
&lt;LI&gt;A document final section property – A document can will only have one of these types of sections &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;In today's post I am going to show you how to remove all sections that are a paragraph property. &lt;/P&gt;
&lt;H2&gt;Solution to Remove Section Breaks &lt;/H2&gt;
&lt;P&gt;To remove section breaks in a document we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open the Word document via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Get access to the main document part &lt;/LI&gt;
&lt;LI&gt;Find all paragraph properties that are contain section breaks &lt;/LI&gt;
&lt;LI&gt;For every paragraph property found, remove the section property as a child of the paragraph property &lt;/LI&gt;
&lt;LI&gt;Save changes &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;For the sake of this example, let's say I am starting with the following Word document: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag3.png" mce_src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag3.png"&gt; &lt;/P&gt;
&lt;P&gt;This document has a section break (shown outlined in red) on the first page, which separates a one column section from a two column section. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;This code is also pretty straight forward and follows the solution steps as described above in the solutions section: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemoveSectionBreaks(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(filename, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt;&amp;gt; paraProps = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(pPr =&amp;gt; IsSectionProps(pPr)).ToList(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt; pPr &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; paraProps) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;pPr.RemoveChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SectionProperties&lt;/SPAN&gt;&amp;gt;(pPr.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SectionProperties&lt;/SPAN&gt;&amp;gt;()); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;mainPart.Document.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsSectionProps(&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt; pPr) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SectionProperties&lt;/SPAN&gt; sectPr = pPr.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SectionProperties&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (sectPr == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Courier New"&gt;else &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Running this code I should end up with a document that looks like the following: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag4.png" mce_src="http://zeyadrajabi.members.winisp.net/061309_0202_RemovingPag4.png"&gt; &lt;/P&gt;
&lt;P&gt;Notice how the document now has two columns. This solution removed the first section property, which specified a one column section. &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9741217" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Word" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Word/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Importing SmartArt from PowerPoint to Word</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/06/04/importing-smartart-from-powerpoint-to-word.aspx</id><published>2009-06-04T16:35:00Z</published><updated>2009-06-04T16:35:00Z</updated><content type="html">&lt;P&gt;In previous posts, I showed you how to &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx"&gt;import charts from spreadsheets to a Wordprocessing document&lt;/A&gt; and how to &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx"&gt;import tables from Word documents to spreadsheets&lt;/A&gt;. Today, I am going to show you how to import SmartArt from a PowerPoint deck into a Word document. &lt;/P&gt;
&lt;P&gt;
&lt;OBJECT height=344 width=425&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/250o8kMvjy4&amp;amp;hl=en&amp;amp;fs=1"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
                                    
            &lt;embed src="http://www.youtube.com/v/250o8kMvjy4&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash"
                allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To import a SmartArt graphic from a PowerPoint deck to a Word document we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a template in Word that contains a content control that will be used to demarcate the region where the SmartArt graphic will be inserted &lt;/LI&gt;
&lt;LI&gt;Open up the PowerPoint deck via the Open XML SDK and access the slide with the SmartArt graphic to be imported &lt;/LI&gt;
&lt;LI&gt;Get all the appropriate parts associated with the SmartArt graphic &lt;/LI&gt;
&lt;LI&gt;Retrieve necessary information from the SmartArt graphic, like name and size &lt;/LI&gt;
&lt;LI&gt;Open up the Word document, which will contain the imported SmartArt graphic &lt;/LI&gt;
&lt;LI&gt;Add all the SmartArt related parts to the Word document &lt;/LI&gt;
&lt;LI&gt;Create a paragraph that contains an inline SmartArt graphic, which references all the added parts &lt;/LI&gt;
&lt;LI&gt;Locate the content control that will contain the SmartArt graphic &lt;/LI&gt;
&lt;LI&gt;Swap out the content control for the created paragraph, which contains the SmartArt graphic &lt;/LI&gt;
&lt;LI&gt;Save changes made to the Word document &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with the following PowerPoint deck and SmartArt graphic: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm1.png" mce_src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm1.png"&gt; &lt;/P&gt;
&lt;P&gt;Also, let's say I am starting with the following document, which contains a placeholder content control: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm2.png" mce_src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm2.png"&gt; &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportSmartArtFromPptToWord.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportSmartArtFromPptToWord.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As described in the solution section above, steps two and three require us to open the PowerPoint deck, find the slide with the SmartArt graphic, and extract all related parts. Below is the code snippet necessary to accomplish those tasks: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt; myPres = &lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt;.Open(presentationFile, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationPart&lt;/SPAN&gt; presPart = myPres.PresentationPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get the slide that contains the SmartArt graphic &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt; slide = (&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt;)presPart.GetPartById(&lt;SPAN style="COLOR: #a31515"&gt;"rId2"&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get all the appropriate parts associated with the SmartArt &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramLayoutDefinitionPart&lt;/SPAN&gt; layoutPart = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;slide.DiagramLayoutDefinitionParts.First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramDataPart&lt;/SPAN&gt; dataPart = slide.DiagramDataParts.First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramColorsPart&lt;/SPAN&gt; colorsPart = slide.DiagramColorsParts.First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramStylePart&lt;/SPAN&gt; stylePart = slide.DiagramStyleParts.First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Note that in the code above I knew which part/slide contained the SmartArt graphic. &lt;/P&gt;
&lt;P&gt;Next step is to retrieve some necessary information from the SmartArt graphic itself. For example, we can retrieve the name, id, extent width, and extent height. The following code snippet retrieves the SmartArt elements that contain all this information (we will use these objects later in the code): &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 622px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get some of the appropriate properties off the SmartArt graphic &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GraphicFrame&lt;/SPAN&gt; graphicFrame = slide.Slide.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;GraphicFrame&lt;/SPAN&gt;&amp;gt;().First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NonVisualDrawingProperties&lt;/SPAN&gt; drawingPr = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;graphicFrame.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;NonVisualDrawingProperties&lt;/SPAN&gt;&amp;gt;().First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Draw.&lt;SPAN style="COLOR: #2b91af"&gt;Extents&lt;/SPAN&gt; extents = graphicFrame.Descendants&amp;lt;Draw.&lt;SPAN style="COLOR: #2b91af"&gt;Extents&lt;/SPAN&gt;&amp;gt;().First();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Now we get to do the fun stuff; import the SmartArt graphic into the Word document. Below is the code snippet necessary to accomplish steps five and six as mentioned in the solution section above: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Import SmartArt into Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(outputFile, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add the SmartArt parts to the Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramLayoutDefinitionPart&lt;/SPAN&gt; docLayoutPart = mainPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramLayoutDefinitionPart&lt;/SPAN&gt;&amp;gt;(layoutPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramDataPart&lt;/SPAN&gt; docDataPart = mainPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramDataPart&lt;/SPAN&gt;&amp;gt;(dataPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramColorsPart&lt;/SPAN&gt; docColorsPart = mainPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramColorsPart&lt;/SPAN&gt;&amp;gt;(colorsPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramStylePart&lt;/SPAN&gt; docStylePart = mainPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DiagramStylePart&lt;/SPAN&gt;&amp;gt;(stylePart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get all the relationship ids of the added parts &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docLayoutPartId = mainPart.GetIdOfPart(docLayoutPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docDataPartId = mainPart.GetIdOfPart(docDataPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docColorsPartId = mainPart.GetIdOfPart(docColorsPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docStylePartId = mainPart.GetIdOfPart(docStylePart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice how we are simply importing the parts from the PowerPoint deck into the Word document. The next step is to create a paragraph object that contains the SmartArt. The easiest way to accomplish this task is to leverage the Document Reflector tool that comes with the SDK. This tool is awesome! It automatically generates SDK code based on a document or content within a document. I would recommend checking out Erika's post on &lt;A href="http://blogs.msdn.com/erikaehrli/archive/2009/05/14/open-xml-format-sdk-2-0-getting-started-best-practices.aspx" mce_href="http://blogs.msdn.com/erikaehrli/archive/2009/05/14/open-xml-format-sdk-2-0-getting-started-best-practices.aspx"&gt;how to use the Document Reflector&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the Document Reflector in action when reading in a Word document containing a SmartArt graphic. Notice the nice C# code it generated for me. &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm3.png" mce_src="http://zeyadrajabi.members.winisp.net/060209_1634_ImportingSm3.png"&gt; &lt;/P&gt;
&lt;P&gt;Copying this code into my solution I get the following: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Use the document reflector to figure out how to add a SmartArt graphic to Word &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Change attribute values based on specifics related to the SmartArt &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Drawing&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; WP.&lt;SPAN style="COLOR: #2b91af"&gt;Inline&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; WP.&lt;SPAN style="COLOR: #2b91af"&gt;Extent&lt;/SPAN&gt;() { Cx = extents.Cx, Cy = extents.Cy }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; WP.&lt;SPAN style="COLOR: #2b91af"&gt;EffectExtent&lt;/SPAN&gt;() { LeftEdge = 0L, TopEdge = 0L, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;RightEdge = 0L, BottomEdge = 0L }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; WP.&lt;SPAN style="COLOR: #2b91af"&gt;DocProperties&lt;/SPAN&gt;() { Id = drawingPr.Id, Name = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;drawingPr.Name }, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; WP.&lt;SPAN style="COLOR: #2b91af"&gt;NonVisualGraphicFrameDrawingProperties&lt;/SPAN&gt;(), &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Draw.&lt;SPAN style="COLOR: #2b91af"&gt;Graphic&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Draw.&lt;SPAN style="COLOR: #2b91af"&gt;GraphicData&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Dgm.&lt;SPAN style="COLOR: #2b91af"&gt;RelationshipIds&lt;/SPAN&gt;() { DataPart = docDataPartId, &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;LayoutPart = docLayoutPartId, StylePart = docStylePartId, ColorPart = docColorsPartId } &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;) { Uri = &lt;SPAN style="COLOR: #a31515"&gt;"http://schemas.openxmlformats.org/drawingml/2006/diagram"&lt;/SPAN&gt; }) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;) { DistanceFromTop = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)0U, DistanceFromBottom = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)0U, DistanceFromLeft = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)0U, DistanceFromRight = (&lt;SPAN style="COLOR: #2b91af"&gt;UInt32Value&lt;/SPAN&gt;)0U })));&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The code snippet above is pretty much a copy/paste. The only difference is that I added the correct extent width and height values as well as the proper name and ids for the SmartArt graphic. &lt;/P&gt;
&lt;P&gt;Almost done! The next thing we need to do is find the content control within the Word document and swap out the content control for this newly created paragraph. The following code snippet accomplishes this task: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 630px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Find the content control in Word where the SmartArt will be added &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;s.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtProperties&lt;/SPAN&gt;&amp;gt;().GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Equals(&lt;SPAN style="COLOR: #a31515"&gt;"SmartArt"&lt;/SPAN&gt;)).First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Swap out the content control for the SmartArt &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;OpenXmlElement&lt;/SPAN&gt; parent = sdt.Parent; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;parent.InsertAfter(p, sdt); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sdt.Remove(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;mainPart.Document.Save();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running this code, we end up with a Word document, which contains the SmartArt imported from my deck. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final document: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/screensmartart.png" mce_src="http://zeyadrajabi.members.winisp.net/screensmartart.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9685890" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Word" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Word/default.aspx" /><category term="PowerPoint" scheme="http://blogs.msdn.com/brian_jones/archive/tags/PowerPoint/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Office 2010 and how it can help you build powerful solutions...</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/06/01/office-2010-and-how-it-can-help-you-build-powerful-solutions.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/06/01/office-2010-and-how-it-can-help-you-build-powerful-solutions.aspx</id><published>2009-06-01T17:29:00Z</published><updated>2009-06-01T17:29:00Z</updated><content type="html">&lt;P&gt;There will be more details over the coming months on what's coming in Office 2010, and the power it brings to developers. I'm particularly excited to start talking about the ways in which you can combine the power of the rich clients with server side technologies to build solutions that no longer just target a specific individual using a specific application, but instead expand out to the workgroup, and all the content they are leveraging to accomplish their goals.&lt;/P&gt;
&lt;P&gt;Many developers are already building solutions that benefit from the combination of SharePoint and the Office rich clients, and I wanted to make sure that folks were aware of the developer conference coming up this fall that will go into all the details of Office 2010. As Gray points out in his latest blog post (&lt;A href="http://blogs.technet.com/gray_knowlton/archive/2009/05/30/office-2010-for-developers-office-developer-conference-moving-to-sharepoint-conference-2009.aspx"&gt;http://blogs.technet.com/gray_knowlton/archive/2009/05/30/office-2010-for-developers-office-developer-conference-moving-to-sharepoint-conference-2009.aspx&lt;/A&gt;), there will be a dedicated track at the SharePoint conference 2009 specifically focused on the rich extensibility model in the latest version of the Office client suite. Many of the features that provide immediate value to the typical end users also will allow developers to build some pretty interesting and powerful custom applications.&lt;/P&gt;
&lt;P&gt;Also, don't forget to sign up for the &lt;A href="http://www.office2010themovie.com/" mce_href="http://www.office2010themovie.com/"&gt;technical preview&lt;/A&gt;!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Brian&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9678863" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Conferences" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Conferences/default.aspx" /></entry><entry><title>Breaking Changes in the Open XML SDK v2 April 2009 CTP</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/05/19/breaking-changes-in-the-open-xml-sdk-v2-april-2009-ctp.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/05/19/breaking-changes-in-the-open-xml-sdk-v2-april-2009-ctp.aspx</id><published>2009-05-19T16:34:00Z</published><updated>2009-05-19T16:34:00Z</updated><content type="html">&lt;P&gt;One of the &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/04/08/announcing-the-release-of-the-open-xml-sdk-version-2-april-2009-ctp.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/04/08/announcing-the-release-of-the-open-xml-sdk-version-2-april-2009-ctp.aspx"&gt;big changes we made in the Open XML SDK v2 April 2009 CTP&lt;/A&gt; was improving the &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/10/06/open-xml-format-sdk-2-0.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/10/06/open-xml-format-sdk-2-0.aspx"&gt;Low Level DOM component &lt;/A&gt;to include functionality related to Office 2007 SP2. With this improvement came a difference in how some elements were interpreted as a 1&lt;SUP&gt;st&lt;/SUP&gt; class property of a parent class/element vs. as a child element. For example, SdtProperties is no longer a property off of the SdtXXX classes, but is rather interpreted as just a child element. Scouring through the different customer feedback channels, the &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads"&gt;Open XML SDK forum&lt;/A&gt;, &lt;A href="http://www.openxmldeveloper.org/" mce_href="http://www.openxmldeveloper.org"&gt;www.openxmldeveloper.org&lt;/A&gt;, and the &lt;A href="https://connect.microsoft.com/feedback/default.aspx?SiteID=589" mce_href="https://connect.microsoft.com/feedback/default.aspx?SiteID=589"&gt;SDK Connect site&lt;/A&gt;, I've noticed that this change broke some of my previous posts and code samples. In today's post, I am going to show you a workaround to this issue and I'm going to point you to April 2009 CTP complaint versions of sample code that is currently broken. &lt;/P&gt;
&lt;H2&gt;SdtBlock does not contain a definition for SdtProperties &lt;/H2&gt;
&lt;P&gt;Many of my Word related SDK posts leverage content controls as semantic structure. In my posts, I show you how to use these content controls to easily find content or add content to a specific area. In many of these posts I used the following code snippet construct: &lt;/P&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; s.SdtProperties.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Equals(sdtName)).First();&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;If you try to compile the above code snippet in the latest CTP of the SDK you will get the following compiler error: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;'DocumentFormat.OpenXml.Wordprocessing.SdtBlock' does not contain a definition for 'SdtProperties' and no extension method 'SdtProperties' accepting a first argument of type 'DocumentFormat.OpenXml.Wordprocessing.SdtBlock' could be found (are you missing a using directive or an assembly reference?) &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To resolve this error you need to get access to SdtProperties as child element rather than a property. This task can be accomplished with the following code snippet: &lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;s.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtProperties&lt;/SPAN&gt;&amp;gt;().GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Equals(sdtName)).First(); &lt;/SPAN&gt;&lt;/P&gt;
&lt;H2&gt;SdtBlock does not contain a definition for SdtContentBlock &lt;/H2&gt;
&lt;P&gt;Similarly to the issue with SdtProperties, SdtContentBlock was moved to be a child element rather than a property. You will need to change the following code snippet from: &lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = sdt.SdtContentBlock.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;To: &lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = sdt.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtContentBlock&lt;/SPAN&gt;&amp;gt;().GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/P&gt;
&lt;H2&gt;Links to Fixed Code Samples/Solutions &lt;/H2&gt;
&lt;P&gt;Here are links to fixed versions of previous solutions that were broken due to April 2009 CTP changes: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 287px"&gt;
&lt;COL style="WIDTH: 160px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR style="BACKGROUND: #4f81bd"&gt;
&lt;TD class="" style="BORDER-RIGHT: medium none; PADDING-RIGHT: 7px; BORDER-TOP: #7ba0cd 1pt solid; PADDING-LEFT: 7px; BORDER-LEFT: #7ba0cd 1pt solid; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;SPAN style="COLOR: white"&gt;&lt;STRONG&gt;Solution&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #7ba0cd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: #7ba0cd 1pt solid; PADDING-LEFT: 7px; BORDER-LEFT: medium none; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;SPAN style="COLOR: white"&gt;&lt;STRONG&gt;Fixed Code Sample Link&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-LEFT: #7ba0cd 1pt solid; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx"&gt;&lt;STRONG&gt;Import tables from Word to Excel&lt;/STRONG&gt;&lt;/A&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #7ba0cd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportTableFromWordToExcel(Fixed).zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportTableFromWordToExcel(Fixed).zip"&gt;Click here&lt;/A&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-LEFT: #7ba0cd 1pt solid; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx"&gt;&lt;STRONG&gt;Import charts from Excel to Word &lt;/STRONG&gt;&lt;/A&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #7ba0cd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-BOTTOM: #7ba0cd 1pt solid"&gt;
&lt;P&gt;&lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportChartFromExcelToWord(Fixed).zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportChartFromExcelToWord(Fixed).zip"&gt;Click here&lt;/A&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;Feedback &lt;/H2&gt;
&lt;P&gt;We are currently looking into improving our SDK behavior to re-include some of the lost functionality of not seeing some elements as 1&lt;SUP&gt;st&lt;/SUP&gt; class properties. Stay tuned for an update. &lt;/P&gt;
&lt;P&gt;I also wanted to take this time to thank you guys for all your feedback. Keep sending it our way. &lt;/P&gt;
&lt;P&gt;Thanks, &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9628506" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Retrieving Word Content Based on Styles</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/05/05/retrieving-word-content-based-on-styles.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/05/05/retrieving-word-content-based-on-styles.aspx</id><published>2009-05-05T22:47:00Z</published><updated>2009-05-05T22:47:00Z</updated><content type="html">&lt;P&gt;In previous posts, like &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx"&gt;Importing a Table from Word to Excel&lt;/A&gt;, I showed you how to retrieve content within specific content controls. In these posts, content controls were used to add semantic structure to a document, where this structure aided in retrieving and inserting content. What about other types of content? Well, one common request is being able to retrieve content based on styles, where content can be paragraphs, runs, or even tables. In other words, styles, too, can be used to add semantic structure and meaning to a document. In today's post, I am going to show you two things: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;How to retrieve content based on styles &lt;/LI&gt;
&lt;LI&gt;How to extend the Open XML SDK with &lt;A href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;Extension Methods&lt;/A&gt; &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Something new I am going to try is to also create a video for my blog posts. Let me know if these videos are helpful to you as well. &lt;/P&gt;
&lt;P&gt;
&lt;OBJECT height=344 width=425&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/yA3TFolIYco&amp;amp;hl=en&amp;amp;fs=1"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
                                    
            &lt;embed src="http://www.youtube.com/v/yA3TFolIYco&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash"
                allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To find Word content based on styles we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open up a Word document via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Get access to the main document part &lt;/LI&gt;
&lt;LI&gt;Find the style id that references the style name. The style id is referenced in paragraphs, runs, and tables &lt;/LI&gt;
&lt;LI&gt;Look for all paragraphs, runs, or tables within the main document part &lt;/LI&gt;
&lt;LI&gt;Filter down the list of paragraphs, runs, or tables based on whether those objects/elements reference a specific style name or not &lt;/LI&gt;
&lt;LI&gt;Return back the final list of paragraphs, runs, or tables &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with the following Word document, which contains Paragraph, Run and Table styles: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/050509_2247_RetrievingW1.png" mce_src="http://zeyadrajabi.members.winisp.net/050509_2247_RetrievingW1.png"&gt; &lt;/P&gt;
&lt;P&gt;In this document, I am using the following styles: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Paragraph Style – Heading 1 &lt;/LI&gt;
&lt;LI&gt;Run Style – Intense Emphasis &lt;/LI&gt;
&lt;LI&gt;Table Style – Light List Accent 1 &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/QueryContentBasedOnStyles.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/QueryContentBasedOnStyles.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;For this solution I thought it would be really cool to take advantage of Extension Methods for C#. Extension methods allow me to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. In my case, I am going to add three extension methods off of the MainDocumentPart class (remember this class represents the main document.xml part within my Word document): &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;ParagraphsByStyleName – This method will retrieve a list of paragraphs contained within the main document part that have a specific style name &lt;/LI&gt;
&lt;LI&gt;RunsByStyleName – This method will retrieve a list of runs contained within the main document part that have a specific style name &lt;/LI&gt;
&lt;LI&gt;TablesByStyleName – This method will retrieve a list of tables contained within the main document part that have a specific style name &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;These three methods are very similar, but have one important difference; these methods all use different strongly typed classes to query for information. These extension methods will live within a class I called WordStyleExtensions. Feel free to reuse or even extend this class for your own purposes. &lt;/P&gt;
&lt;P&gt;Since styles are referenced via ids on paragraphs, runs, and tables, we need a way to look up the style id from a style name. The following code accomplishes this task for any style: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; GetStyleIdFromStyleName(&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;StyleDefinitionsPart&lt;/SPAN&gt; stylePart = mainPart.StyleDefinitionsPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId = stylePart.Styles.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;StyleName&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; s.Val.Value.Equals(styleName)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Select(n =&amp;gt; ((&lt;SPAN style="COLOR: #2b91af"&gt;Style&lt;/SPAN&gt;)n.Parent).StyleId).FirstOrDefault(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; styleId ?? styleName; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;This code simply looks up the style id from a style name. If one is not found then the style name is returned. &lt;/P&gt;
&lt;P&gt;Let's dive down into the code for retrieving paragraphs based on a style name. As described in the solution section above, this task is broken down into two steps. The first step is to retrieve all paragraphs in the main document, which can be accomplished with the following code: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt; ParagraphsByStyleName(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId = GetStyleIdFromStyleName(mainPart, styleName); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt; paraList = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(p =&amp;gt; IsParagraphInStyle(p, styleId)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; paraList; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The next step is to filter down the paragraphs based on whether the paragraph uses a specific style name. This task can accomplished with the following code: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 582px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;private static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsParagraphInStyle(&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt; pPr = p.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphProperties&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (pPr != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ParagraphStyleId&lt;/SPAN&gt; paraStyle = pPr.ParagraphStyleId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (paraStyle != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; paraStyle.Val.Value.Equals(styleId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Pretty simple! The cool thing is that these methods can be easily modified to work with runs and tables. Here are the methods to retrieve content based on run and table styles: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;&amp;gt; RunsByStyleName(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId = GetStyleIdFromStyleName(mainPart, styleName);&lt;SPAN style="COLOR: #2b91af"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;&amp;gt; runList = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(r =&amp;gt; IsRunInStyle(r, styleId)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; runList; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;private static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsRunInStyle(&lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt; r, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RunProperties&lt;/SPAN&gt; rPr = r.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;RunProperties&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (rPr != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RunStyle&lt;/SPAN&gt; runStyle = rPr.RunStyle; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (runStyle != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; runStyle.Val.Value.Equals(styleId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt;&amp;gt; TablesByStyleName(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId = GetStyleIdFromStyleName(mainPart, styleName);&lt;SPAN style="COLOR: #2b91af"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt;&amp;gt; tableList = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(t =&amp;gt; IsTableInStyle(t, styleId)); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; tableList; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;private static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsTableInStyle(&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt; tbl, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; styleId) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TableProperties&lt;/SPAN&gt; tblPr = tbl.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;TableProperties&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (tblPr != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TableStyle&lt;/SPAN&gt; tblStyle = tblPr.TableStyle; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (tblStyle != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; tblStyle.Val.Value.Equals(styleId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Now that our extension methods have been created all we have left to do is call these methods: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Main(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] args) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; paraStyle = &lt;SPAN style="COLOR: #a31515"&gt;"Heading1"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; runStyle = &lt;SPAN style="COLOR: #a31515"&gt;"IntenseEmphasis"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; tableStyle = &lt;SPAN style="COLOR: #a31515"&gt;"LightList-Accent1"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(&lt;SPAN style="COLOR: #a31515"&gt;"input.docx"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Number of paragraphs with "&lt;/SPAN&gt; + paraStyle + &lt;SPAN style="COLOR: #a31515"&gt;" styles: "&lt;/SPAN&gt; + mainPart.ParagraphsByStyleName(paraStyle).Count()); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Number of runs with "&lt;/SPAN&gt; + runStyle + &lt;SPAN style="COLOR: #a31515"&gt;" styles: "&lt;/SPAN&gt; + mainPart.RunsByStyleName(runStyle).Count()); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Number of tables with "&lt;/SPAN&gt; + tableStyle + &lt;SPAN style="COLOR: #a31515"&gt;" styles: "&lt;/SPAN&gt; + mainPart.TablesByStyleName(tableStyle).Count()); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.ReadKey(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running this code, we end up with an easy way to retrieve content based on styles. For simplicity sake, I decided to just show the number of paragraphs, runs, or tables with a specific style. &lt;/P&gt;
&lt;P&gt;Here is the output: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/050509_2247_RetrievingW2.png" mce_src="http://zeyadrajabi.members.winisp.net/050509_2247_RetrievingW2.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9589879" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Word" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Word/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Removing Comments from Excel and PowerPoint Files</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/04/20/removing-comments-from-excel-and-powerpoint-files.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/04/20/removing-comments-from-excel-and-powerpoint-files.aspx</id><published>2009-04-20T22:42:00Z</published><updated>2009-04-20T22:42:00Z</updated><content type="html">&lt;P&gt;In a previous post, I showed you &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/02/06/removing-comments-from-a-wordprocessing-document-programmatically.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/02/06/removing-comments-from-a-wordprocessing-document-programmatically.aspx"&gt;how to remove comments from a Word file&lt;/A&gt;. In today's post, I am going to show you how to accomplish the same scenario, but this time with Excel and PowerPoint files. Excel and PowerPoint have a Document Inspector feature, which is able to remove multiple types of data/content, including comments. This feature works great for client side solutions, but how do you cleanse these files of comments on the server? The Open XML SDK can accomplish these scenarios in just a few lines of code. &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: The code showed in this post is backwards compatible with &lt;A href="http://go.microsoft.com/fwlink/?LinkId=120908" mce_href="http://go.microsoft.com/fwlink/?LinkId=120908"&gt;version 1 of the SDK&lt;/A&gt;. &lt;/EM&gt;&lt;/P&gt;
&lt;H2&gt;Solution for PowerPoint &lt;/H2&gt;
&lt;P&gt;Imagine I start off with a PowerPoint deck that has multiple slides and comments. Here is a screenshot of a comment within a PowerPoint slide: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom1.png" mce_src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom1.png"&gt; &lt;/P&gt;
&lt;P&gt;To remove comments from a PowerPoint deck we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open up the PowerPoint deck via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Access the main presentation part, which will give us access to all the slide parts within the package &lt;/LI&gt;
&lt;LI&gt;Delete the comment part associated with each slide part &lt;/LI&gt;
&lt;LI&gt;Save changes made to the deck &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;If you want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/RemovePowerPointComments.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/RemovePowerPointComments.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H3&gt;The Code &lt;/H3&gt;
&lt;P&gt;The code is pretty easy and maps 1:1 to the steps I mentioned above: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemovePowerPointComments(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt; myPresentation = &lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt;.Open(filename, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationPart&lt;/SPAN&gt; presentationPart = myPresentation.PresentationPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt; slide &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; presentationPart.SlideParts) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;slide.DeletePart(slide.SlideCommentsPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H3&gt;End Result &lt;/H3&gt;
&lt;P&gt;Running this method, I end up with a presentation void of comments. Pretty easy! &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final presentation: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom2.png" mce_src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom2.png"&gt; &lt;/P&gt;
&lt;H2&gt;Solution for Excel &lt;/H2&gt;
&lt;P&gt;This solution is very similar to the PowerPoint solution. &lt;/P&gt;
&lt;P&gt;Imagine I start off with an Excel workbook that has multiple worksheets, where each worksheet contains multiple comments. Here is a screenshot of a comment within a worksheet cell: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom3.png" mce_src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom3.png"&gt; &lt;/P&gt;
&lt;P&gt;To remove comments from an Excel workbook we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open up the Excel workbook via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Access the main workbook part, which will give us access to all the worksheet parts within the package &lt;/LI&gt;
&lt;LI&gt;Delete all comment parts associated with each worksheet part &lt;/LI&gt;
&lt;LI&gt;Save changes made to the workbook &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;If you want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/RemoveExcelComments.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/RemoveExcelComments.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H3&gt;The Code &lt;/H3&gt;
&lt;P&gt;This code is very similar to the code used to remove comments from a PowerPoint deck: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 638px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 1px; BORDER-LEFT: black 0.5pt solid; PADDING-TOP: 1px; BORDER-BOTTOM: black 0.5pt solid" vAlign=center&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemoveExcelComments(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt; myWorkbook = &lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt;.Open(filename, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; workbookPart = myWorkbook.WorkbookPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; sheet &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; workbookPart.WorksheetParts) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheet.DeleteParts&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetCommentsPart&lt;/SPAN&gt;&amp;gt;(sheet.GetPartsOfType&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetCommentsPart&lt;/SPAN&gt;&amp;gt;()); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H3&gt;End Result &lt;/H3&gt;
&lt;P&gt;Running this method, I end up with a workbook void of comments. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final workbook: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom4.png" mce_src="http://zeyadrajabi.members.winisp.net/042009_2242_RemovingCom4.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9557592" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Excel" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Excel/default.aspx" /><category term="PowerPoint" scheme="http://blogs.msdn.com/brian_jones/archive/tags/PowerPoint/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="SpreadsheetML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/SpreadsheetML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Open XML is now a US National Standard</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/04/16/open-xml-is-now-a-us-national-standard.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/04/16/open-xml-is-now-a-us-national-standard.aspx</id><published>2009-04-16T16:19:00Z</published><updated>2009-04-16T16:19:00Z</updated><content type="html">&lt;P&gt;More on Doug's blog: &lt;A href="http://blogs.msdn.com/dmahugh/archive/2009/04/17/is29500-is-an-american-national-standard.aspx"&gt;http://blogs.msdn.com/dmahugh/archive/2009/04/17/is29500-is-an-american-national-standard.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;-Brian&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9553102" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Office Open XML Standard" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Office+Open+XML+Standard/default.aspx" /><category term="Standards Politics" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Standards+Politics/default.aspx" /></entry><entry><title>Announcing the Release of the Open XML SDK Version 2 April 2009 CTP</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/04/08/announcing-the-release-of-the-open-xml-sdk-version-2-april-2009-ctp.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/04/08/announcing-the-release-of-the-open-xml-sdk-version-2-april-2009-ctp.aspx</id><published>2009-04-08T21:07:00Z</published><updated>2009-04-08T21:07:00Z</updated><content type="html">&lt;P&gt;I'm really happy to announce the release of the &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;second CTP for version 2 of the Open XML SDK&lt;/A&gt;! Back in &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/10/06/open-xml-format-sdk-2-0.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/10/06/open-xml-format-sdk-2-0.aspx"&gt;October 2008&lt;/A&gt; I showed you guys an architecture diagram of the Open XML SDK. Let's take another look at this diagram: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt1.png" mce_src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt1.png"&gt; &lt;/P&gt;
&lt;P&gt;In version 1 of the Open XML SDK we released the Open XML Packaging API, which allows you to create, open and manipulate Open XML files at the package and part level. In the first CTP of version 2 we released the Open XML Low Level DOM and Stream Reading/Writing components, which allow you to create and manipulate objects within xml parts contained in an Open XML package. In the second CTP of version 2 we are providing schema level validation functionality: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt2.png" mce_src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt2.png"&gt; &lt;/P&gt;
&lt;P&gt;More information about the SDK and all of its components can be found here: &lt;A href="http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx&lt;/SPAN&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Download Details &lt;/H2&gt;
&lt;P&gt;You can download the latest CTP for version 2 here: &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;http://go.microsoft.com/fwlink/?LinkId=127912&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Importance of Validation &lt;/H2&gt;
&lt;P&gt;If you've played around with the Open XML SDK there is a good chance at one point in time your solution has created an invalid or corrupt Open XML file. Manipulating Open XML Formats by using the Open XML Base layer makes it much easier for you to work on the Open XML files, but doing so does not guarantee the production of valid Open XML files. &lt;/P&gt;
&lt;P&gt;The new Schema Level Validation component provides a mechanism to help you discover Open XML errors within files and in your code. This component assists you in debugging and validating Open XML files based on the schemas. &lt;/P&gt;
&lt;H2&gt;An Example &lt;/H2&gt;
&lt;P&gt;Let's say I want to create a simple document with the text "hello world." The XML snippet necessary to create this text is the following: &lt;/P&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:p&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 80px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:r&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 100px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:t&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;hello world&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:t&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 80px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:r&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:p&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;P&gt;What happens if I forget to include the run element: &lt;/P&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:p&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 80px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:t&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;hello world&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:t&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:p&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 40px"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;w:body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;P&gt;If I try to open the generated document in Word I will get the following error: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt3.png" mce_src="http://zeyadrajabi.members.winisp.net/040809_2106_Announcingt3.png"&gt; &lt;/P&gt;
&lt;P&gt;Not super helpful, right. Validating this file with .NET I get the following error message: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;\word\document.xml &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The element 'p' in namespace 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' has invalid child element 't' in namespace 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'. List of possible elements expected: 'pPr, customXml, smartTag, sdt, dir, bdo, r, proofErr, permStart, permEnd, bookmarkStart, bookmarkEnd, moveFromRangeStart, moveFromRangeEnd, moveToRangeStart, moveToRangeEnd, commentRangeStart, commentRangeEnd, customXmlInsRangeStart, customXmlInsRangeEnd, customXmlDelRangeStart, customXmlDelRangeEnd, customXmlMoveFromRangeStart, customXmlMoveFromRangeEnd, customXmlMoveToRangeStart, customXmlMoveToRangeEnd, ins, del, moveFrom, moveTo' in namespace 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' as well as 'oMathPara, oMath' in namespace 'http://schemas.openxmlformats.org/officeDocument/2006/math' as well as 'fldSimple, hyperlink, subDoc' in namespace 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'. (line 1, col 703) &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes this error message is pretty useful, but the Open XML SDK schema validation component provides a bit more useful information. Here is the error message as described by the Open XML SDK: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The element has invalid child element "&lt;A href="http://schemas.openxmlformats.org/wordprocessingml/2006/main:t" mce_href="http://schemas.openxmlformats.org/wordprocessingml/2006/main:t"&gt;http://schemas.openxmlformats.org/wordprocessingml/2006/main:t&lt;/A&gt;". List of possible elements expected: &amp;lt;&lt;A href="http://schemas.openxmlformats.org/wordprocessingml/2006/main:pPr" mce_href="http://schemas.openxmlformats.org/wordprocessingml/2006/main:pPr"&gt;http://schemas.openxmlformats.org/wordprocessingml/2006/main:pPr&lt;/A&gt;&amp;gt;. Path: /word/document.xml:/w:document[1]/w:body[1]/w:p[1] OuterXml: &amp;lt;w:p w:rsidR="00900A7A" w:rsidRDefault="00E0086A" xmlns:w="&lt;A href="http://schemas.openxmlformats.org/wordprocessingml/2006/main" mce_href="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;http://schemas.openxmlformats.org/wordprocessingml/2006/main&lt;/A&gt;"&amp;gt;&amp;lt;w:t&amp;gt;test&amp;lt;/w:t&amp;gt;&amp;lt;/w:p&amp;gt; &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Notice that the error gives you the exact XPath to the element and XML snippet that triggered this validation error. There are more advantages, but I will leave talking about those advantages for a future post. &lt;/P&gt;
&lt;H2&gt;Questions &lt;/H2&gt;
&lt;P&gt;Feel free to post any questions here or on the Open XML SDK MSDN forum: &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads"&gt;http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads&lt;/A&gt;. You can also post questions on the Open XML SDK Connect site: &lt;A href="https://connect.microsoft.com/site/sitehome.aspx?SiteID=589" mce_href="https://connect.microsoft.com/site/sitehome.aspx?SiteID=589"&gt;https://connect.microsoft.com/site/sitehome.aspx?SiteID=589&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9538946" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Importing a Table from WordprocessingML to SpreadsheetML</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/04/01/importing-a-table-from-wordprocessingml-to-spreadsheetml.aspx</id><published>2009-04-02T00:45:00Z</published><updated>2009-04-02T00:45:00Z</updated><content type="html">&lt;P&gt;In a previous post, I showed you how to &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx"&gt;import charts from spreadsheets to a Wordprocessing document&lt;/A&gt;. Today, as promised in my &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/03/26/more-open-xml-solution-tools.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/03/26/more-open-xml-solution-tools.aspx"&gt;last post&lt;/A&gt;, I am going to show you how to import a table from a Wordprocessing document into a spreadsheet. &lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To import a table from a Wordprocessing document to a spreadsheet we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a template in Word that contains a content control that will be used to demarcate the region of the table to be imported &lt;/LI&gt;
&lt;LI&gt;Open up the Word document via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Open up an output Excel workbook, which will be used to contain the imported table, via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Locate the content control that contains the Word table and access the table object &lt;/LI&gt;
&lt;LI&gt;Convert the Word table into an Excel spreadsheet table. By convert I am mean manually create an Excel table based on the Word table data &lt;/LI&gt;
&lt;LI&gt;Add a table definition part within the Excel workbook &lt;/LI&gt;
&lt;LI&gt;Give the added table definition a unique name and id &lt;/LI&gt;
&lt;LI&gt;Specify the range of the table based on the inserted data &lt;/LI&gt;
&lt;LI&gt;Add column header information to the table definitions part &lt;/LI&gt;
&lt;LI&gt;Add a style reference to the table definition &lt;/LI&gt;
&lt;LI&gt;Save changes made to the Excel workbook &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with the following Word document and table (notice that the table is contained within a content control): &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT1.png" mce_src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT1.png"&gt; &lt;/P&gt;
&lt;P&gt;Also, let's say I am starting with a completely empty Excel workbook, which only contains a style definition for tables: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT2.png" mce_src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT2.png"&gt; &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportTableFromWordToExcel.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportTableFromWordToExcel.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As described in the solution section above, steps two and three require us to open the Word and Excel files. Below is the code snippet necessary to accomplish these tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(&lt;SPAN style="COLOR: #a31515"&gt;"tables.docx"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open spreadhseet &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt; mySpreadsheet = &lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt;.Open(&lt;SPAN style="COLOR: #a31515"&gt;"output.xlsx"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; workbookPart = mySpreadsheet.WorkbookPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Import a specific table from the document into a specific sheet within the spreadsheet &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;ImportWordTable(mainPart, &lt;SPAN style="COLOR: #a31515"&gt;"WorldPopulationTable"&lt;/SPAN&gt;, workbookPart, &lt;SPAN style="COLOR: #a31515"&gt;"Sheet1"&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;I created a generic method called ImportWordTable, which will accomplish the rest of the steps in the solution section above. The next step is to get access to the Word table object found within the content control, which can be accomplished with the following code: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Find the content control that contains the specific table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; s.SdtProperties.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Equals(sdtName)).First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt; tbl = sdt.SdtContentBlock.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt;&amp;gt;();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have access to the Word table our next task is to create an Excel table based on the data contained within the Word table. This task can be accomplished with the following code snippet: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;SheetData&lt;/SPAN&gt; ConvertWordTableToExcel(&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt; tbl) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//sheetdata contains data for table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;SheetData&lt;/SPAN&gt; sheetdata = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;SheetData&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//For every row in my Word table we need to create a row in Excel &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;TableRow&lt;/SPAN&gt; row &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; tbl.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;TableRow&lt;/SPAN&gt;&amp;gt;()) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;numRows++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Row&lt;/SPAN&gt; sheetRow = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Row&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//For every Word cell create an Excel cell &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;TableCell&lt;/SPAN&gt; cell &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; row.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;TableCell&lt;/SPAN&gt;&amp;gt;()) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Cell&lt;/SPAN&gt; sheetCell = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Cell&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; textValue = cell.InnerText; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; numValue; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Check to see if value is a number rather than a string &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//First row should be strings because I am going to create a table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; ((numRows != 1) &amp;amp;&amp;amp; (&lt;SPAN style="COLOR: #2b91af"&gt;Double&lt;/SPAN&gt;.TryParse(textValue, System.Globalization.&lt;SPAN style="COLOR: #2b91af"&gt;NumberStyles&lt;/SPAN&gt;.Any, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt; numValue))) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;CellValue&lt;/SPAN&gt; v = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;CellValue&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;textValue = numValue.ToString(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;v.Text = textValue; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheetCell.Append(v); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;//value is a string &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheetCell.DataType = Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;CellValues&lt;/SPAN&gt;.InlineString; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;InlineString&lt;/SPAN&gt; inlineString = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;InlineString&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Text&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Text&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;t.Text = textValue; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;inlineString.Append(t); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheetCell.Append(inlineString); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Need to keep track of column headers for the table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//definitions part &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (numRows == 1) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;colHeaders.Add(textValue); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheetRow.Append(sheetCell); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheetdata.Append(sheetRow); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; sheetdata; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The code snippet above simply creates an Excel spreadsheet row and cell for every Word table row and cell. A spreadsheet cell is a bit special as compared to a Word cell because a spreadsheet cell defines the datatype of the cell content. In the example above, we have two types of spreadsheet cells: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Inline string cells &lt;/LI&gt;
&lt;LI&gt;Number value cells &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Since this solution is about creating an Excel table, we need to take care of a couple of extra things. For one, we need to make sure that the first row of the table has only inline string cells, since we will be identifying this row as our table header. Secondly, we need to keep track of the header row strings because we need to reuse these strings within the table definitions part. &lt;/P&gt;
&lt;P&gt;At the end of this step we have generated a valid Excel spreadsheet that contains all of the data from the Word table: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT3.png" mce_src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT3.png"&gt; &lt;/P&gt;
&lt;P&gt;We could have called this job done at this point, but we can further improve this output by formatting the data as a table. &lt;/P&gt;
&lt;P&gt;Looking at step #6 in the solution section above, we need to add a reference to a table definitions part within the worksheet where the data exists. Here is the code snippet that creates a new table definition part and adds a reference to that part from the worksheet that contains the table data: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 30pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add the table definitions part to make the imported table look like a table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TableDefinitionPart&lt;/SPAN&gt; tableDefPart = worksheetPart.AddNewPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;TableDefinitionPart&lt;/SPAN&gt;&amp;gt;(relId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Reference the added table part &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableParts&lt;/SPAN&gt; tableParts = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableParts&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TablePart&lt;/SPAN&gt; tablePart = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TablePart&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tablePart.Id = relId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableParts.Append(tablePart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;worksheetPart.Worksheet.Append(tableParts);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The last major set of steps is to create the appropriate xml within our new table definitions part. Every table definition needs a unique name, unique id, display name, and appropriate data range reference in order to get everything to work properly. The following code snippet accomplishes these tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; id = 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;Table&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Id = 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Name = &lt;SPAN style="COLOR: #a31515"&gt;"Table1"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.DisplayName = &lt;SPAN style="COLOR: #a31515"&gt;"Table1"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt; endCol = &lt;SPAN style="COLOR: #a31515"&gt;'A'&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Note that this approach is good for columns up to Z &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Excel can contain more columns, but 26 columns should &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//be good enough for this demo &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;for&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; i = 1; i &amp;lt; numCols; i++) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;endCol++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Specify the range of the table &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; reference = &lt;SPAN style="COLOR: #a31515"&gt;"A1:"&lt;/SPAN&gt; + endCol + numRows; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Reference = reference;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;One advantage of an Excel table vs. a Word table is an Excel table allows you to filter values within a column. It's pretty easy to add this functionality to our table with the following code: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Make sure the table has the ability to filter &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;AutoFilter&lt;/SPAN&gt; autoFilter = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;AutoFilter&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;autoFilter.Reference = reference; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableColumns&lt;/SPAN&gt; tableColumns = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableColumns&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableColumns.Count = numCols;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Now let's add the table header information: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add the column headers to the table definition part &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; s &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; colHeaders) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableColumn&lt;/SPAN&gt; tableColumn = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableColumn&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableColumn.Id = (&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt;)id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableColumn.Name = s; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;id++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableColumns.Append(tableColumn); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Almost done! Let's make this table look good by applying a table style. Once we apply the style we can append all the appropriate elements together and save our changes. These tasks can be accomplished with the following code: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Apply a nice table style (contained within my template) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableStyleInfo&lt;/SPAN&gt; tableStyleInfo = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Spreadsheet.&lt;SPAN style="COLOR: #2b91af"&gt;TableStyleInfo&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableStyleInfo.Name = &lt;SPAN style="COLOR: #a31515"&gt;"TableStyleMedium9"&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableStyleInfo.ShowRowStripes = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Append(autoFilter, tableColumns, tableStyleInfo); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Save();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running this code, we end up with an Excel workbook, which contains a table imported from my Word document. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final workbook (notice the filtering capability): &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT4.png" mce_src="http://zeyadrajabi.members.winisp.net/040209_0044_ImportingaT4.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9528255" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Word" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Word/default.aspx" /><category term="Excel" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Excel/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="SpreadsheetML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/SpreadsheetML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>More Open XML Solution Tools</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/03/26/more-open-xml-solution-tools.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/03/26/more-open-xml-solution-tools.aspx</id><published>2009-03-26T22:07:00Z</published><updated>2009-03-26T22:07:00Z</updated><content type="html">&lt;P&gt;While I finish up another blog solution, this time on importing a table from Word into Excel, I thought I would share some information on two useful tools you guys can leverage when building Open XML solutions. The first tool I want to talk about is the next release of PowerTools for Open XML. PowerTools for Open XML is an &lt;A href="http://www.codeplex.com/PowerTools" mce_href="http://www.codeplex.com/PowerTools"&gt;open source project on CodePlex&lt;/A&gt;, which is entirely based on version 1 of the Open XML SDK. This tool supports the &lt;A href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" mce_href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx"&gt;PowerShell &lt;/A&gt;piping architecture, by providing 30+ cmdlets. Think of a cmdlet as a black box, where something comes in and something else comes out. In the case of PowerTools for Open XML, these cmdlets create and modify Open XML documents in a variety of ways. For example, there are cmdlets that remove comments, accept tracked changes, merge multiple documents or sections, and even create charts. One of the coolest things about this tool is that IT professionals and developers can perform batch scripts right within PowerShell. By the way, this tool is released as open source, under the Microsoft Public License (Ms-PL), which means you are free to use the code in your own solutions. &lt;/P&gt;
&lt;P&gt;Eric White has a great blog post &lt;A href="http://blogs.msdn.com/ericwhite/archive/2009/03/19/announcing-the-release-of-powertools-for-open-xml-v1-1.aspx" mce_href="http://blogs.msdn.com/ericwhite/archive/2009/03/19/announcing-the-release-of-powertools-for-open-xml-v1-1.aspx"&gt;outlining some of the details with respect to version 1.1 of the PowerTools for Open XML&lt;/A&gt;. In this post he has some great examples showing you how to merge sections of documents together. In my previous post, I showed you the &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx"&gt;easy way to assemble multiple Word documents by taking advantage of altChunks&lt;/A&gt;. Version 1.1 of the PowerTools for Open XML actually does merging the hard way. Check it out and let us know what you think. &lt;/P&gt;
&lt;P&gt;The next tool I want to talk about is around creating Office document solutions within native code, like C++. The Open XML SDK is a great tool, but is essentially tied down to .NET technologies because of its reliance on System.IO.Packaging. How do you create a solution using native code? In the past you would either have to write your own native -&amp;gt; managed transition layer or do some kind of context switch. The good news is Microsoft has just released a &lt;A href="http://msdn.microsoft.com/en-us/library/dd371623.aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd371623.aspx"&gt;Win32 version of System.IO.Packaging&lt;/A&gt;, called Win32 OPC. Feel free to post any questions about this technology at the following &lt;A href="http://social.msdn.microsoft.com/Forums/en-us/os_opc/threads/" mce_href="http://social.msdn.microsoft.com/Forums/en-us/os_opc/threads/"&gt;forum&lt;/A&gt; or at the &lt;A href="http://blogs.msdn.com/opc_buzz/" mce_href="http://blogs.msdn.com/opc_buzz/"&gt;Win32 OPC blog&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Thanks, &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9512144" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Importing Charts from Spreadsheets to Wordprocessing Documents</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx</id><published>2009-03-13T17:25:00Z</published><updated>2009-03-13T17:25:00Z</updated><content type="html">&lt;P&gt;There have been several requests made by people asking how to import a chart from one document type to another document type. In a previous post, I showed you &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/11/04/document-assembly-solution-for-spreadsheetml.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/11/04/document-assembly-solution-for-spreadsheetml.aspx"&gt;how to push data into a spreadsheet to create a chart&lt;/A&gt;. Today, I am going to show you how to import a chart from a spreadsheet to a Word document. &lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To import a chart from a spreadsheet to a Word document we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a template in Word that contains a content control that will be used to demarcate the region where the chart will be inserted &lt;/LI&gt;
&lt;LI&gt;Open up the Word document via the Open XML SDK and access its main document part &lt;/LI&gt;
&lt;LI&gt;Locate the content control that will contain the chart &lt;/LI&gt;
&lt;LI&gt;Nuke any placeholder content within the content control &lt;/LI&gt;
&lt;LI&gt;Create a new run and inline drawing object to be inserted in the content control. This inline drawing will contain the referencing information for the chart &lt;/LI&gt;
&lt;LI&gt;Open up the spreadsheet via the Open XML SDK and access all the appropriate parts (main workbook part, worksheet part, drawing part, and chart part) &lt;/LI&gt;
&lt;LI&gt;Clone the chart part and add it to the Word document &lt;/LI&gt;
&lt;LI&gt;Clone the chart graphic information (name of the chart and properties) from the spreadsheet and add it to the Word document &lt;/LI&gt;
&lt;LI&gt;Give the added chart a unique name and id in the Word document &lt;/LI&gt;
&lt;LI&gt;Add the chart data to the content control &lt;/LI&gt;
&lt;LI&gt;Save changes made to the Word document &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with the following spreadsheet and chart: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh1.png" mce_src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh1.png"&gt; &lt;/P&gt;
&lt;P&gt;Also, let's say I am starting with the following Word document, which contains a placeholder content control: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh2.png" mce_src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh2.png"&gt; &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportChartFromExcelToWord.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/ImportChartFromExcelToWord.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As described in the solution section above, steps two through four require us to open the Word document, find the content control that will contain the imported chart, and nuke the placeholder content of the content control to get it ready to contain the chart. Below is the code snippet necessary to accomplish those tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ImportChartFromSpreadsheet(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; spreadsheetFileName, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; wordFileName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt; myWordDoc = &lt;SPAN style="COLOR: #2b91af"&gt;WordprocessingDocument&lt;/SPAN&gt;.Open(wordFileName, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Find the content control that will contain the chart &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart = myWordDoc.MainDocumentPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt; sdt = mainPart.Document.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SdtBlock&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; a.SdtProperties.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Alias&lt;/SPAN&gt;&amp;gt;().Val.Value &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Equals(&lt;SPAN style="COLOR: #a31515"&gt;"Chart1"&lt;/SPAN&gt;)).First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Nuke the placeholder content of the content control &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt; p = sdt.SdtContentBlock.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Paragraph&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;p.RemoveAllChildren(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;You might notice the code used to find the proper content control looks very much like the code I used in my previous post for &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx"&gt;assembling Word documents&lt;/A&gt;. By nuking all the children under the paragraph within the content control, we have made it ready to contain a chart. &lt;/P&gt;
&lt;P&gt;Instead of having run and text elements, the content control needs to have a run with an inline drawing element so that we can reference the chart properly. This task is accomplished with the following code snippet: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Create a new run that has an inline drawing object &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt; r = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Run&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;p.Append(r); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Drawing&lt;/SPAN&gt; drawing = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Drawing&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;r.Append(drawing); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//These dimensions work perfectly for my template document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;wp.&lt;SPAN style="COLOR: #2b91af"&gt;Inline&lt;/SPAN&gt; inline = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; wp.&lt;SPAN style="COLOR: #2b91af"&gt;Inline&lt;/SPAN&gt;( &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; wp.&lt;SPAN style="COLOR: #2b91af"&gt;Extent&lt;/SPAN&gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ Cx = 5486400, Cy = 3200400 }); &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice that I have hardcoded the dimensions of the chart I will be importing. The dimensions I used are based on what I thought would work best for my template. Feel free to choose any dimension that works best for your document. At this point, the Word document is ready to have the chart imported and be contained within the content control. To import the chart we need to first get the proper chart part and then add that part to my Word document. These tasks are accomplished with the following code snippet: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class=style1 style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid" class="style1"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open Excel spreadsheet &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt; mySpreadsheet = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt;.Open(spreadsheetFileName, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get all the appropriate parts &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; workbookPart = mySpreadsheet.WorkbookPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; worksheetPart = (&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt;)workbookPart.GetPartById(&lt;SPAN style="COLOR: #a31515"&gt;"rId1"&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DrawingsPart&lt;/SPAN&gt; drawingPart = worksheetPart.DrawingsPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ChartPart&lt;/SPAN&gt; chartPart = (&lt;SPAN style="COLOR: #2b91af"&gt;ChartPart&lt;/SPAN&gt;)drawingPart.GetPartById(&lt;SPAN style="COLOR: #a31515"&gt;"rId1"&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Clone the chart part and add it to my Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ChartPart&lt;/SPAN&gt; importedChartPart = mainPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ChartPart&lt;/SPAN&gt;&amp;gt;(chartPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; relId = mainPart.GetIdOfPart(importedChartPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;...&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The chart part is now part of my Word document package. All we have to do now is reference the chart part properly from the inline drawing contained in the content control. There are two main items we need to include to get everything to work: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;A graphic reference to the chart part &lt;/LI&gt;
&lt;LI&gt;A unique id and a chart name needs to be specified for each chart &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;#1 is easy to accomplish since we can reuse the same graphic objects from the spreadsheet. The main difference is we need to make sure the relationship is pointing to the appropriate chart part within the Word document. Here is the code snippet to accomplish item #1: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//The frame element contains information for the chart &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GraphicFrame&lt;/SPAN&gt; frame = &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;drawingPart.WorksheetDrawing.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;GraphicFrame&lt;/SPAN&gt;&amp;gt;().First(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Clone this node so we can add it to my Word document &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;d.&lt;SPAN style="COLOR: #2b91af"&gt;Graphic&lt;/SPAN&gt; clonedGraphic = (d.&lt;SPAN style="COLOR: #2b91af"&gt;Graphic&lt;/SPAN&gt;)frame.Graphic.CloneNode(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ChartReference&lt;/SPAN&gt; c = clonedGraphic.GraphicData.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ChartReference&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;c.Id = relId;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;#2 requires us to calculate a unique id for the chart. Here is a method that can accomplish this task: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; GetMaxDocPrId(&lt;SPAN style="COLOR: #2b91af"&gt;MainDocumentPart&lt;/SPAN&gt; mainPart) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; max = 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get max id value of docPr elements &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (wp.&lt;SPAN style="COLOR: #2b91af"&gt;DocProperties&lt;/SPAN&gt; docPr &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; mainPart.Document.Descendants&amp;lt;wp.&lt;SPAN style="COLOR: #2b91af"&gt;DocProperties&lt;/SPAN&gt;&amp;gt;()) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; id = docPr.Id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (id &amp;gt; max) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;max = id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; max; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Almost done! The last bit of work we need to do is add the unique name and id to chart drawing and then add everything to the inline drawing object. Here is the code snippet to accomplish these tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Give the chart a unique id and name &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;wp.&lt;SPAN style="COLOR: #2b91af"&gt;DocProperties&lt;/SPAN&gt; docPr = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; wp.&lt;SPAN style="COLOR: #2b91af"&gt;DocProperties&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docPr.Name = chartName; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;docPr.Id = GetMaxDocPrId(mainPart) + 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//add the chart data to the inline drawing object &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;inline.Append(docPr, clonedGraphic); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;drawing.Append(inline);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running this code, we end up with a Word document, which contains the chart imported from my spreadsheet. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final document: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh3.png" mce_src="http://zeyadrajabi.members.winisp.net/031309_1725_ImportingCh3.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9473422" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Word" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Word/default.aspx" /><category term="Excel" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Excel/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="SpreadsheetML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/SpreadsheetML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>How to Assemble Multiple PowerPoint Decks</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/03/05/how-to-assemble-multiple-powerpoint-decks.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/03/05/how-to-assemble-multiple-powerpoint-decks.aspx</id><published>2009-03-05T19:04:00Z</published><updated>2009-03-05T19:04:00Z</updated><content type="html">&lt;P&gt;&lt;EM&gt;I just want to let you guys know we are working on some server issues here, which is why some of the previous posts are not showing images or links to source code. This issue will hopefully be resolved soon.&lt;/EM&gt; &lt;/P&gt;
&lt;P&gt;In my previous post, I showed you the &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx"&gt;easy way to assemble multiple Word documents&lt;/A&gt;. Today, I am going to show you how to assemble multiple PowerPoint decks together. &lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To merge two decks, a source deck and a destination deck, together we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open up the destination deck via the Open XML SDK and access its main presentation part &lt;/LI&gt;
&lt;LI&gt;Open up the source deck and access its main presentation part &lt;/LI&gt;
&lt;LI&gt;Add every slide in the source deck, in order, to the destination deck &lt;/LI&gt;
&lt;LI&gt;For every slide added to the destination deck, make sure that there is a relationship between the main destination presentation part and the copied master slide layout part &lt;/LI&gt;
&lt;LI&gt;For every added master slide layout part make sure there is a reference to that part within the main destination part &lt;/LI&gt;
&lt;LI&gt;For every added slide part make sure there is a reference to that part within the main destination part &lt;/LI&gt;
&lt;LI&gt;Perform cleanup work to ensure that all references to slide layout parts have unique ids &lt;/LI&gt;
&lt;LI&gt;Save changes made to the destination deck &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with the following two decks, each with three slides: &lt;/P&gt;
&lt;DIV&gt;
&lt;TABLE class="" style="BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 319px"&gt;
&lt;COL style="WIDTH: 319px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR style="BACKGROUND: #edf2f8"&gt;
&lt;TD class="" style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: #4f81bd 1pt solid; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
&lt;P&gt;&lt;SPAN style="COLOR: black"&gt;&lt;STRONG&gt;Destination Deck&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: #4f81bd 1pt solid; PADDING-LEFT: 7px; BORDER-LEFT: medium none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
&lt;P&gt;&lt;SPAN style="COLOR: black"&gt;&lt;STRONG&gt;Source Deck&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="BACKGROUND: #a7bfde"&gt;
&lt;TD class="" style="BORDER-RIGHT: #4f81bd 0.75pt solid; PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-LEFT: #4f81bd 1pt solid; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
&lt;P&gt;&lt;IMG alt="Destination deck with three slides" src="http://stunna42.members.winisp.net/030509_1909_HowtoAssemb1.png"&gt; &lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #4f81bd 1pt solid; PADDING-RIGHT: 7px; BORDER-TOP: medium none; PADDING-LEFT: 7px; BORDER-LEFT: medium none; BORDER-BOTTOM: #4f81bd 1pt solid"&gt;
&lt;P&gt;&lt;IMG alt="Source deck with three slides" src="http://stunna42.members.winisp.net/030509_1909_HowtoAssemb2.png"&gt; &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://stunna42.members.winisp.net/sourcecode/AssemblePowerPointDecks.zip" mce_href="http://stunna42.members.winisp.net/sourcecode/AssemblePowerPointDecks.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As described in the solution section above, the first three steps require us to open both the destination and source decks in order to add every slide in the source deck to the destination deck. Below is the code snippet necessary to accomplish those tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 671px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; MergeDecks(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sourceDeck, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; destDeck) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; id = 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open up the destination deck &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt; myDestDeck = &lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt;.Open(destDeck, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationPart&lt;/SPAN&gt; destPresPart = myDestDeck.PresentationPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open up the source deck &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt; mySourceDeck = &lt;SPAN style="COLOR: #2b91af"&gt;PresentationDocument&lt;/SPAN&gt;.Open(sourceDeck, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationPart&lt;/SPAN&gt; sourcePresPart = mySourceDeck.PresentationPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Need to get a unique ids for slide master and slide lists //(will use these later) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;uniqueId = GetMaxIdFromChild(destPresPart.Presentation.SlideMasterIdList); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; maxSlideId = GetMaxIdFromChild(destPresPart.Presentation.SlideIdList); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Copy each slide in my source deck in order to my destination deck &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SlideId&lt;/SPAN&gt; slideId &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sourcePresPart.Presentation.SlideIdList) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt; sp; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt; destSp; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlideMasterPart&lt;/SPAN&gt; destMasterPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; relId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlideMasterId&lt;/SPAN&gt; newSlideMasterId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlideId&lt;/SPAN&gt; newSlideId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//come up with a unique relationship id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;id++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sp = (&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt;)sourcePresPart &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 180pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.GetPartById(slideId.RelationshipId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;relId = sourceDeck.Remove(sourceDeck.IndexOf(&lt;SPAN style="COLOR: #a31515"&gt;'.'&lt;/SPAN&gt;)) + id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;destSp = destPresPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt;&amp;gt;(sp, relId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: green"&gt;//Add the slide part to the destination deck&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt; destSp = destPresPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SlidePart&lt;/SPAN&gt;&amp;gt;(sp, relId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 144pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;We needed to go through the slide id list in order to ensure that we added all the slides from the source deck to the destination deck in order. In addition, note that we also &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/02/19/how-to-copy-a-worksheet-within-a-workbook.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/02/19/how-to-copy-a-worksheet-within-a-workbook.aspx"&gt;taking advantage of AddPart&lt;/A&gt;, which allows us to not only add a slide part, but all parts referenced by that slide part. &lt;/P&gt;
&lt;P&gt;At this point we have just imported the slide part to the destination deck. Our next task is to fix up the relationship between the main destination presentation part to the added slide master part. Again, we are going to take advantage of AddPart to add this relationship with the following code (AddPart will fix up relationships if the part already exists in the package): &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Master part was added, but now we need to make sure the relationship is in place &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;destMasterPart = destSp.SlideLayoutPart.SlideMasterPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;destPresPart.AddPart(destMasterPart);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;The next steps are to add the list of slide master part ids and slide ids to the appropriate lists in the main destination presentation part. The ids referenced in each of these lists need to be unique. An additional constraint is that the id values across the slide master id and the slide layout id lists need to be unique and are required to have id values that are greater than or equal to 2147483684. To help deal with the uniqueness factor we need a method that is able to return the current max id value in the list. So anytime we add a new item to the list we simply use the max id value and add 1. Below is a generic method that is able to retrieve the max id value in a set of children elements: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; GetMaxIdFromChild(&lt;SPAN style="COLOR: #2b91af"&gt;OpenXmlElement&lt;/SPAN&gt; el) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; max = 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get max id value from set of children &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;OpenXmlElement&lt;/SPAN&gt; child &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; el.ChildElements) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;OpenXmlAttribute&lt;/SPAN&gt; attribute = child.GetAttribute(&lt;SPAN style="COLOR: #a31515"&gt;"id"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;""&lt;/SPAN&gt;); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt; id = &lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt;.Parse(attribute.Value); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (id &amp;gt; max) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;max = id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; max; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;This method is called before the initial for loop in order to get the appropriate unique ids for the slide master part id and slide id lists. Once we have the unique ids we can add the slide master part id and slide id to the appropriate lists in the main destination presentation part. Below is the necessary code needed to add to these two lists: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add slide master to slide master list &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;uniqueId++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideMasterId = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SlideMasterId&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideMasterId.Id = uniqueId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add slide to slide list &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;maxSlideId++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideId = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SlideId&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideId.RelationshipId = relId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;newSlideId.Id = maxSlideId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); destPresPart.Presentation.SlideIdList.Append(newSlideId); &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Almost done! The last bit of work we need to do is ensure that all slide layout ids are unique. As mentioned above these id values cannot conflict with the id values of the slide master part id list. In addition we need to make sure that these ids are greater than 2147483684. Below is a method that will go through all slide master parts and will fix up all referenced slide layout ids by simply incrementing id values based on the maximum seen uint value seen thus far: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; FixSlideLayoutIds(&lt;SPAN style="COLOR: #2b91af"&gt;PresentationPart&lt;/SPAN&gt; presPart) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Need to make sure all slide layouts have unique ids &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SlideMasterPart&lt;/SPAN&gt; slideMasterPart &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; presPart.SlideMasterParts) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SlideLayoutId&lt;/SPAN&gt; slideLayoutId &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; slideMasterPart.SlideMaster.SlideLayoutIdList) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;uniqueId++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;slideLayoutId.Id = (&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt;)uniqueId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;slideMasterPart.SlideMaster.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running this code, we end up with a presentation that has six slides, all in the proper order. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final presentation: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Final merged deck with six slides" src="http://stunna42.members.winisp.net/030509_1909_HowtoAssemb3.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9460410" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="PowerPoint" scheme="http://blogs.msdn.com/brian_jones/archive/tags/PowerPoint/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>How to Copy a Worksheet within a Workbook</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/02/19/how-to-copy-a-worksheet-within-a-workbook.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/02/19/how-to-copy-a-worksheet-within-a-workbook.aspx</id><published>2009-02-19T18:54:00Z</published><updated>2009-02-19T18:54:00Z</updated><content type="html">&lt;P&gt;As promised in my previous post about &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/02/13/reusable-methods-for-manipulating-paragraphs-in-wordprocessingml.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/02/13/reusable-methods-for-manipulating-paragraphs-in-wordprocessingml.aspx"&gt;reusable methods for manipulating WordprocessingML&lt;/A&gt;, I mentioned I would write a post on how to copy a worksheet within a workbook. Note that this blog post talks about copying a worksheet within the same package. Perhaps sometime in the future I will write a post on how to export/import worksheets across different workbooks. &lt;/P&gt;
&lt;H2&gt;Solution &lt;/H2&gt;
&lt;P&gt;To copy a worksheet within a workbook we need to take the following actions: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open up the Spreadsheet document via the Open XML SDK &lt;/LI&gt;
&lt;LI&gt;Access the main workbook part, which will give us access to a bunch of related parts, like the different worksheets &lt;/LI&gt;
&lt;LI&gt;Access the worksheet we want to copy &lt;/LI&gt;
&lt;LI&gt;Clone the found worksheet plus all related parts and add the clone plus all related parts back to the workbook &lt;/LI&gt;
&lt;LI&gt;Perform cleanup work to ensure that tables, views, etc. work &lt;/LI&gt;
&lt;LI&gt;Add the newly created worksheet reference to the sheets list in the main workbook part &lt;/LI&gt;
&lt;LI&gt;Save changes made to the workbook &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;My post will talk about using &lt;A href="http://go.microsoft.com/fwlink/?LinkId=127912" mce_href="http://go.microsoft.com/fwlink/?LinkId=127912"&gt;version 2 of the SDK&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;For the sake of this post, let's say I am starting with a pretty complex workbook, which contains data, conditional formatting, a shape, an image, a table, a SmartArt, and a chart. The workbook contains three worksheets and looks like the following: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Screenshot of sample Excel workbook" src="http://zeyadrajabi.members.winisp.net/screen1.png"&gt; &lt;/P&gt;
&lt;P&gt;If you just want to jump straight into the code, feel free to download this solution &lt;A href="http://zeyadrajabi.members.winisp.net/sourcecode/CopyWorksheet.zip" mce_href="http://zeyadrajabi.members.winisp.net/sourcecode/CopyWorksheet.zip"&gt;here&lt;/A&gt;. &lt;/P&gt;
&lt;H2&gt;AddPart&amp;lt;T&amp;gt;() vs. AddNewPart&amp;lt;T&amp;gt;() &lt;/H2&gt;
&lt;P&gt;Before we get into the details of the steps listed above, I wanted to take this opportunity to discuss the difference between two methods that the SDK provides for adding parts to an Open XML package. The AddNewPart method does the following: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It creates an empty part of type T and adds it to the package &lt;/LI&gt;
&lt;LI&gt;Once the part is created it then adds a reference from the referencing part to the new part &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The next step after adding a new part via this method is usually calling FeedData() to stream in data into the part. &lt;/P&gt;
&lt;P&gt;The AddPart method does the following: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If the added part is not already in the package, it will add that part plus all of its related parts to the package. So if you are adding part A and part A references part B, which in turns references part C, then calling this method will add part A, part B, and part C. In addition it will ensure that each of these added parts will maintain their relationships. You can think of this functionality as something similar to a deep clone import &lt;/LI&gt;
&lt;LI&gt;If the added part is already in the package, it will add a reference from the referencing part to the part already in the package. For example, let's say you have part A and part B in the package, but part A does not reference part B. Calling this method will then add a reference from part A to part B if the Open XML format supports such a reference &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;As you can see AddPart is a lot more powerful than simply calling AddNewPart. This fact will be useful when I show you how to clone a part within a package. &lt;/P&gt;
&lt;H2&gt;The Code &lt;/H2&gt;
&lt;P&gt;As described in the solution section above, the first three steps require us to open the workbook and get access to the worksheet we want to copy. Below are the code snippets necessary to accomplish those tasks: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CopySheet(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sheetName, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; clonedSheetName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Open workbook &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt; mySpreadsheet = &lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt;.Open(filename, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; workbookPart = mySpreadsheet.WorkbookPart; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get the source sheet to be copied &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; sourceSheetPart = GetWorkSheetPart(workbookPart, sheetName); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;...&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Below is the snippet necessary to get a worksheet part based on the sheet name: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; GetWorkSheetPart(&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; workbookPart, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sheetName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Get the relationship id of the sheetname &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; relId = workbookPart.Workbook.Descendants&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Sheet&lt;/SPAN&gt;&amp;gt;() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Where(s =&amp;gt; s.Name.Value.Equals(sheetName)) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.First() &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;.Id; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt;)workbookPart.GetPartById(relId); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have access to the worksheet part we want to copy, we need to perform our clone task. Well, here is where I am going to take advantage of our AddPart functionality. Perhaps in a future build of our SDK we will actually have a clone method for parts. As mentioned above, AddPart is great at adding a part plus all referenced parts. Unfortunately, this functionality only works when adding a part that does not already exist in a package. Well, to work around this issue we can simply call AddPart to a temporary workbook and then call AddPart again back into the main workbook. The following code accomplishes this task: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CopySheet(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sheetName, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; clonedSheetName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Take advantage of AddPart for deep cloning &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt; tempSheet = &lt;SPAN style="COLOR: #2b91af"&gt;SpreadsheetDocument&lt;/SPAN&gt;.Create(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MemoryStream&lt;/SPAN&gt;(), mySpreadsheet.DocumentType); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorkbookPart&lt;/SPAN&gt; tempWorkbookPart = tempSheet.AddWorkbookPart(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; tempWorksheetPart = tempWorkbookPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt;&amp;gt;(sourceSheetPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add cloned sheet and all associated parts to workbook &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; clonedSheet = workbookPart.AddPart&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt;&amp;gt;(tempWorksheetPart); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;...&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;At this point in time, we have successfully cloned the worksheet and added it plus all related parts into the workbook. We are almost done... &lt;/P&gt;
&lt;P&gt;The next thing we need to do is perform a couple of cleanup tasks. For example, SpreadsheetML requires that every table has a unique name and id. In addition, there really should be only one worksheet that is set as the main view. The following code shows you how to clean these issues up: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CopySheet(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sheetName, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; clonedSheetName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Table definition parts are somewhat special and need unique ids...so let's make an id based on count &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; numTableDefParts = sourceSheetPart.GetPartsCountOfType&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;TableDefinitionPart&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableId = numTableDefParts; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Clean up table definition parts (tables need unique ids) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (numTableDefParts != 0) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 108pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;FixupTableParts(clonedSheet, numTableDefParts); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//There should only be one sheet that has focus &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;CleanView(clonedSheet); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Clean the view means just remove any view reference in the cloned worksheet. &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CleanView(&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; worksheetPart) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//There can only be one sheet that has focus &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SheetViews&lt;/SPAN&gt; views = worksheetPart.Worksheet.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;SheetViews&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (views != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;views.Remove(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;worksheetPart.Worksheet.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Fix up the table parts simply means make sure each table has a unique id and name. &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; FixupTableParts(&lt;SPAN style="COLOR: #2b91af"&gt;WorksheetPart&lt;/SPAN&gt; worksheetPart, &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; numTableDefParts) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Every table needs a unique id and name &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;TableDefinitionPart&lt;/SPAN&gt; tableDefPart &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; worksheetPart.TableDefinitionParts) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableId++; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Id = (&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt;)tableId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.DisplayName = &lt;SPAN style="COLOR: #a31515"&gt;"CopiedTable"&lt;/SPAN&gt; + tableId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Name = &lt;SPAN style="COLOR: #a31515"&gt;"CopiedTable"&lt;/SPAN&gt; + tableId; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;tableDefPart.Table.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Alright, last step is to add a reference to the added worksheet in the main workbook part with the following code: &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 36pt"&gt;
&lt;TABLE class="" style="BACKGROUND: #dbe5f1; BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 631px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: black 0.5pt solid; PADDING-RIGHT: 7px; BORDER-TOP: black 0.5pt solid; PADDING-LEFT: 7px; BORDER-LEFT: black 0.5pt solid; BORDER-BOTTOM: black 0.5pt solid"&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; CopySheet(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; sheetName, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; clonedSheetName) &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;{ &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Add new sheet to main workbook part &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Sheets&lt;/SPAN&gt; sheets = workbookPart.Workbook.GetFirstChild&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Sheets&lt;/SPAN&gt;&amp;gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Sheet&lt;/SPAN&gt; copiedSheet = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Sheet&lt;/SPAN&gt;(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;copiedSheet.Name = clonedSheetName; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;copiedSheet.Id = workbookPart.GetIdOfPart(clonedSheet); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;copiedSheet.SheetId = (&lt;SPAN style="COLOR: blue"&gt;uint&lt;/SPAN&gt;)sheets.ChildElements.Count + 1; &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;sheets.Append(copiedSheet); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;//Save Changes &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;workbookPart.Workbook.Save(); &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV style="MARGIN-LEFT: 72pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;... &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;H2&gt;End Result &lt;/H2&gt;
&lt;P&gt;Putting everything together and running my code, we end up with a workbook that has four sheets, where the last sheet in the workbook, called CopiedData, is an exact replica of the first sheet. &lt;/P&gt;
&lt;P&gt;Here is a screenshot of the final workbook: &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Screenshot of Excel workbook after running my code" src="http://zeyadrajabi.members.winisp.net/screen2.png"&gt; &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9434684" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="Excel" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Excel/default.aspx" /><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="Code Samples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Code+Samples/default.aspx" /><category term="SpreadsheetML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/SpreadsheetML/default.aspx" /><category term="Zeyad's SDK Examples" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Zeyad_2700_s+SDK+Examples/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry><entry><title>Reusable Methods for Manipulating Paragraphs in WordprocessingML</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/brian_jones/archive/2009/02/13/reusable-methods-for-manipulating-paragraphs-in-wordprocessingml.aspx" /><id>http://blogs.msdn.com/brian_jones/archive/2009/02/13/reusable-methods-for-manipulating-paragraphs-in-wordprocessingml.aspx</id><published>2009-02-13T19:16:00Z</published><updated>2009-02-13T19:16:00Z</updated><content type="html">&lt;P&gt;In a previous &lt;A href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx"&gt;post&lt;/A&gt;, I showed you guys the easy way to merge multiple Word documents into one final document by taking advantage of altChunks. One issue with using altChunks is in order to view the final merged document you need an application, like Word, that understands altChunks and is able to actually perform the complex merge tasks. What happens if you don't have the luxury of using Word, or any other application that understands altChunks? Well, then you are required to manually merge the documents together. &lt;/P&gt;
&lt;P&gt;Manually merging content within the same or a different document is possible, but requires you to deal with certain issues. There are a number of things you need to consider before you can call your merge task complete. Here are a few example complexities: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;STRONG&gt;Styles&lt;/STRONG&gt; &lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;Does your content reference any styles? &lt;/LI&gt;
&lt;LI&gt;Does your destination document also reference those styles? &lt;/LI&gt;
&lt;LI&gt;Are there any style conflicts between the source and destination documents? For example, does the source document specify bold for style "Foo" while the destination document specify italics for style "Foo?" &lt;/LI&gt;
&lt;LI&gt;Are the document defaults and Normal style definitions different between the source and destination files? &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;STRONG&gt;Numbering&lt;/STRONG&gt; &lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;Does your content reference any numbering? &lt;/LI&gt;
&lt;LI&gt;Does your source document reference a numbering definition that already exists in the destination document? &lt;/LI&gt;
&lt;LI&gt;Do you want continue numbering or restart numbering for copied content? &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;STRONG&gt;References &lt;/STRONG&gt;&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;Does your content reference other parts, like images, comments, headers/footers, etc.? &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;STRONG&gt;Range Elements &lt;/STRONG&gt;&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;Does your content contain range based elements, like bookmarks, content controls, custom xml, etc.? &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The issues listed above are just some of the things you need to think about before you can accomplish manual merging. Sounds like a lot of work, but I do have some good news. &lt;/P&gt;
&lt;P&gt;Eric White recently wrote a &lt;A href="http://blogs.msdn.com/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx" mce_href="http://blogs.msdn.com/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx"&gt;post&lt;/A&gt;, where he talks about how we have extended the &lt;A href="http://www.codeplex.com/PowerTools" mce_href="http://www.codeplex.com/PowerTools"&gt;Power Tools for Open XML &lt;/A&gt;to include functionality around manipulating, inserting, and deleting paragraphs within a Wordprocessing document. The great thing about these Power Tools is that they are completely open source under the Microsoft Public License (Ms-PL). That means you can freely deploy solutions that use any of the code within the Power Tools. Another cool piece of information is that these tools are built off of version 1 of the SDK, which has a "go-live" license. In other words, there is nothing stopping you from reusing any of the code within Power Tools for your own solution. Perhaps in a later post I will build on top of these libraries to accomplish a rich end-to-end scenario. &lt;/P&gt;
&lt;H2&gt;Next Time &lt;/H2&gt;
&lt;P&gt;In my last &lt;A href="http://blogs.msdn.com/brian_jones/archive/2009/02/06/removing-comments-from-a-wordprocessing-document-programmatically.aspx" mce_href="http://blogs.msdn.com/brian_jones/archive/2009/02/06/removing-comments-from-a-wordprocessing-document-programmatically.aspx"&gt;post&lt;/A&gt;, I got a request from Anthony Rubalcaba to write a post on copying a spreadsheet within a workbook. So, next week I will show you how to accomplish this scenario. &lt;/P&gt;
&lt;P&gt;Zeyad Rajabi &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9419708" width="1" height="1"&gt;</content><author><name>BrianJones</name><uri>http://blogs.msdn.com/members/BrianJones.aspx</uri></author><category term="OpenXML Developers" scheme="http://blogs.msdn.com/brian_jones/archive/tags/OpenXML+Developers/default.aspx" /><category term="WordprocessingML" scheme="http://blogs.msdn.com/brian_jones/archive/tags/WordprocessingML/default.aspx" /><category term="Open XML SDK" scheme="http://blogs.msdn.com/brian_jones/archive/tags/Open+XML+SDK/default.aspx" /></entry></feed>