<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alkampfer&#039;s Place&#187; Office</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/programming/office/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet</description>
	<lastBuildDate>Tue, 27 Jul 2010 10:16:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Open Xml Project</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/11/18/open-xml-project/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/11/18/open-xml-project/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:40:46 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[.NET framework]]></category>
		<category><![CDATA[Office]]></category>
		<category><![CDATA[OpenXml]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/11/18/open-xml-project/</guid>
		<description><![CDATA[
			
				
			
		
I’ve received some comments in an old post, regarding the manipulation of docx documents, with the purpose of substitute parts of the original document with images or pieces of text. I’ve blogged also how to use Excel to create reports, and populate data programmatically with OpenXml standard.
The original code was developed for an Italian company [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F11%2F18%2Fopen-xml-project%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F11%2F18%2Fopen-xml-project%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I’ve received some comments in an old <a href="http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/">post</a>, regarding the manipulation of docx documents, with the purpose of substitute parts of the original document with <a href="http://www.codewrecks.com/blog/index.php/2008/09/08/manage-image-in-openxml-format-part2/">images</a> or pieces of text. I’ve blogged also how to use <a href="http://www.codewrecks.com/blog/index.php/2008/11/28/create-a-report-in-excel-2007-with-open-xml-sdk-10/">Excel</a> to create reports, and populate data programmatically with OpenXml standard.</p>
<p>The original code was developed for an Italian company named <a target="_blank" href="http://www.actvalue.com">ActValue</a> I collaborate with, and some people asked me to publish the full code. I cannot publish the exact version of the current library, but, thanks to the courtesy of <a target="_blank" href="http://www.actvalue.com">ActValue</a>, I can now publish an old version of the code, that contains all the techniques I’ve described in my old post about <a href="http://www.codewrecks.com/blog/?s=openxml">OpenXml</a> Format.</p>
<p>Please use the code only as a reference to better understand the technique I explained in my blog, this is not fully ready production code. You can do everything you want with it, this is the disclaimer.</p>
<blockquote><p>Copyright (c) 2009 Ricci Gian Maria     <br />All rights reserved.      <br />Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:      <br />* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.      <br />* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.      <br />* Neither the name of the author (Ricci Gian Maria) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.      <br />THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. </p>
</blockquote>
<p>Please remember that nor I nor Actvalue will be give any warranties on this code, as described in the above license. Please feel free to signal me any malfunction.</p>
<p>The code can be <a href="http://www.codewrecks.com/Files/Codewrecks_openxml.zip">downloaded here</a>.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/OpenXml" rel="tag">OpenXml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/11/18/open-xml-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenXml excel and formulas</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/06/16/openxml-excel-and-formulas/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/06/16/openxml-excel-and-formulas/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 16:10:13 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/06/16/openxml-excel-and-formulas/</guid>
		<description><![CDATA[
			
				
			
		
In an old post, I deal with a simple way to create excel report using openXml format. The trick is a simple manipulation of the document with Linq to Xml. 
Now I need to add another feature, I need to open an excel document with formulas, fill some cells, leaving formulas intact. My first version [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F16%2Fopenxml-excel-and-formulas%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F16%2Fopenxml-excel-and-formulas%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In an <a href="http://www.codewrecks.com/blog/index.php/2008/11/28/create-a-report-in-excel-2007-with-open-xml-sdk-10/">old post</a>, I deal with a simple way to create excel report using openXml format. The trick is a simple manipulation of the document with Linq to Xml. </p>
<p>Now I need to add another feature, I need to open an excel document with formulas, fill some cells, leaving formulas intact. My first version does not work as expected, I simply created an excel with simple formulas, then fire my function and when I open the resulting excel I see all zero on formula column, but the formula is there, and if I change some cell referenced by the formula I’ll obtain the right value.</p>
<p>This problem arise because formula are stored in original sheet with such a xml</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2d6f34d9-f8cf-46a7-be76-ef6319962782" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">c </span><span style="color: #FF0000;">r</span><span style="color: #0000FF;">="C2"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">f </span><span style="color: #FF0000;">t</span><span style="color: #0000FF;">="shared"</span><span style="color: #FF0000;"> ref</span><span style="color: #0000FF;">="C2:C10"</span><span style="color: #FF0000;"> si</span><span style="color: #0000FF;">="0"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">A2+B2</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">f</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">v</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">0</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">v</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">c</span><span style="color: #0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This means that the cell C2 contains the formula A2+B2, but the node &lt;v&gt;0&lt;/v&gt; tells Excel that actual value is Zero. So when you open the resulting excel file, excel found that content of the cell is 0 and shows this value until some related cell changes content. To solve this problem I simply added a bit of code that removes the &lt;v&gt; element.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ba7855ba-516b-4dbc-aa8f-fd88ee1efdc7" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">originalElement.Descendants(ExcelFiller.ns_s </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">v</span><span style="color: #800000;">"</span><span style="color: #000000;">).Remove();
row.Add(originalElement);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now when excel opens elaborated document, it found no &lt;v&gt; (value) element, so it recalculate it based on formula.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/OpenXml" rel="tag">OpenXml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/06/16/openxml-excel-and-formulas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage image in openXml format part2</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/09/08/manage-image-in-openxml-format-part2/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/09/08/manage-image-in-openxml-format-part2/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 10:55:46 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/09/08/manage-image-in-openxml-format-part2/</guid>
		<description><![CDATA[
			
				
			
		
In a previous post I deal with image insertion into an openXml document. Now it is time to show how to change image dimension, I want to be able to define new dimension and to choose if the image should be stretched or no. The w:drawing element has two distinct part to manage image dimension, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F08%2Fmanage-image-in-openxml-format-part2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F08%2Fmanage-image-in-openxml-format-part2%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://www.codewrecks.com/blog/index.php/2008/09/02/openxml-format-insert-an-image-into-a-document/">a previous post</a> I deal with image insertion into an openXml document. Now it is time to show how to change image dimension, I want to be able to define new dimension and to choose if the image should be stretched or no. The <em>w:drawing </em>element has two distinct part to manage image dimension, the first is the <em>wp:extent </em>node, child of the wp:inline one.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="96" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image-thumb2.png" width="497" border="0"></a> </p>
<p>This node determines the extent of the area of the document that will contain the image, but to really change dimension of the image we should operate in a different tag:</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image3.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="200" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image-thumb3.png" width="356" border="0"></a> </p>
<p>The <em>spPr </em>node is used to determines the shape properties as described in the section 4.4.1.41 of the specification of the openXml format, it contains the <em>xfrm</em> node, that is used to apply 2D transformation to an object. This particular node is used to specify the offset of the picture into the area, and the ext is used to set the real image dimension. The code to change image width is the following</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a087ed74-a46d-4ca4-9be3-4e715540d5f9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">String nodeContent </span><span style="color: #000000;">=</span><span style="color: #000000;">
    Properties.Resources.XmlContentForEmbedImage.Replace(
        </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###imageid###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">,
        document.WordProcessingDocument.MainDocumentPart.GetIdOfPart(newImage))
        .Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###width###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (Width </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString())
        .Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###height###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (Height </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString());
nodeContent </span><span style="color: #000000;">=</span><span style="color: #000000;"> SetImageDimension(nodeContent);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>I store a sample of the image xml code in the resource file of the project, then I simply change image Id as described in the previous post, finally I change the width and height of the wp:extent. Since the value of these tag are to be expressed in EMUs (that is English Metric Units and not the <a target="_blank" href="http://www.planetozkids.com/oban/animals/facts-emu.htm">famous animal</a>) I multiply for 9525, a constant that converts from pixel unit to EMUs. The function SetImageDimension sets the a:xfrm node.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:25cce6c8-06b3-46a5-ad19-92d6c8b72202" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> SetImageDimension(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> nodeContent)
{
    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (StretchImage)
    {
        nodeContent </span><span style="color: #000000;">=</span><span style="color: #000000;"> nodeContent.Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###widthr###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (Width </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString())
                .Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###heightr###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (Height </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString());
    }
    </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
    {
        Double widthRatio </span><span style="color: #000000;">=</span><span style="color: #000000;"> OriginalWidth </span><span style="color: #000000;">/</span><span style="color: #000000;"> (Double) Width;
        Double heightRatio </span><span style="color: #000000;">=</span><span style="color: #000000;"> OriginalHeight </span><span style="color: #000000;">/</span><span style="color: #000000;"> (Double) Height;
        Double realRatio </span><span style="color: #000000;">=</span><span style="color: #000000;"> Math.Max(widthRatio, heightRatio);
        nodeContent </span><span style="color: #000000;">=</span><span style="color: #000000;"> nodeContent.Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###widthr###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (OriginalWidth </span><span style="color: #000000;">*</span><span style="color: #000000;"> realRatio </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString())
                            .Replace(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###heightr###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, (OriginalHeight </span><span style="color: #000000;">*</span><span style="color: #000000;"> realRatio </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">9525</span><span style="color: #000000;">).ToString());
    }
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> nodeContent;
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As you can see in the sample image code I write ###widthr### and ###heightr### tag to set the correct format, if the StretchImage is set to true, I simply set the width and height as specified from the user, if it is false I should make some simple calculation to avoid image stretching.</p>
<p>Now I&#8217;m able to resize the image as desidered.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/docx" rel="tag">docx</a> <a target="_blank" href="http://technorati.com/tag/openXml Format" rel="tag">openXml Format</a> </p>
<div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:f1a936ba-4b6b-44ca-bb5b-7082cc8026b1" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><!--dotnetkickit--></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/09/08/manage-image-in-openxml-format-part2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OpenXml format, insert an image into a document</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/09/02/openxml-format-insert-an-image-into-a-document/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/09/02/openxml-format-insert-an-image-into-a-document/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 07:51:06 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/09/02/openxml-format-insert-an-image-into-a-document/</guid>
		<description><![CDATA[
			
				
			
		
In previous post I showed how to open a docx file, search for a specific text, and replace the text with another string. The reason for doing this is simply to create a master report file in docx format, and let an application insert data in specific part of the document itself.
The next step is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F02%2Fopenxml-format-insert-an-image-into-a-document%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F02%2Fopenxml-format-insert-an-image-into-a-document%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/">previous post</a> I showed how to open a docx file, search for a specific text, and replace the text with another string. The reason for doing this is simply to create a master report file in docx format, and let an application insert data in specific part of the document itself.</p>
<p>The next step is to substitute text with an image, this is a more complex process, because we need first to insert the image into the package, then we need to reference it in the main document. The first part is really simple</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:6db73c76-461f-43d0-a461-618c6552ebb1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">ImagePart newImage </span><span style="color: #000000;">=</span><span style="color: #000000;"> document.MainDocumentPart.AddImagePart(imageType);
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (Stream image </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> FileStream(imageFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    newImage.FeedData(image);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This code is a part of a little library I&#8217;m developing, in the first line I use the AddImagePart method of the MainDocumentPart, that creates another part of the document that will contain an image, then I simply open a fileStream to read the image data and use the method FeedData of the ImagePart object. Now we have the image included in the document.</p>
<p>The next step is to add a reference to the image into the document, to accomplish this I store an XML fragment in project resources </p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:d6b280f6-ea90-4bf0-bc88-ed0c5a39fb46" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">root     </span><span style="color: #FF0000;">xmlns:ve</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span><span style="color: #FF0000;">
    xmlns:o</span><span style="color: #0000FF;">=&quot;urn:schemas-microsoft-com:office:office&quot;</span><span style="color: #FF0000;">
    xmlns:r</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/officeDocument/2006/relationships&quot;</span><span style="color: #FF0000;">
    xmlns:m</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/officeDocument/2006/math&quot;</span><span style="color: #FF0000;">
    xmlns:v</span><span style="color: #0000FF;">=&quot;urn:schemas-microsoft-com:vml&quot;</span><span style="color: #FF0000;">
    xmlns:wp</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing&quot;</span><span style="color: #FF0000;">
    xmlns:w10</span><span style="color: #0000FF;">=&quot;urn:schemas-microsoft-com:office:word&quot;</span><span style="color: #FF0000;">
    xmlns:w</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;</span><span style="color: #FF0000;">
    xmlns:wne</span><span style="color: #0000FF;">=&quot;http://schemas.microsoft.com/office/word/2006/wordml&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">w:drawing</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">wp:inline </span><span style="color: #FF0000;">distT</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> distB</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> distL</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> distR</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">wp:extent </span><span style="color: #FF0000;">cx</span><span style="color: #0000FF;">=&quot;955040&quot;</span><span style="color: #FF0000;"> cy</span><span style="color: #0000FF;">=&quot;955040&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">wp:effectExtent </span><span style="color: #FF0000;">l</span><span style="color: #0000FF;">=&quot;19050&quot;</span><span style="color: #FF0000;"> t</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> r</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> b</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">wp:docPr </span><span style="color: #FF0000;">id</span><span style="color: #0000FF;">=&quot;1&quot;</span><span style="color: #FF0000;"> name</span><span style="color: #0000FF;">=&quot;Immagine 1&quot;</span><span style="color: #FF0000;"> descr</span><span style="color: #0000FF;">=&quot;&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">wp:cNvGraphicFramePr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:graphicFrameLocks </span><span style="color: #FF0000;">xmlns:a</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/drawingml/2006/main&quot;</span><span style="color: #FF0000;"> noChangeAspect</span><span style="color: #0000FF;">=&quot;1&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">wp:cNvGraphicFramePr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:graphic </span><span style="color: #FF0000;">xmlns:a</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/drawingml/2006/main&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:graphicData </span><span style="color: #FF0000;">uri</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/drawingml/2006/picture&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">pic:pic </span><span style="color: #FF0000;">xmlns:pic</span><span style="color: #0000FF;">=&quot;http://schemas.openxmlformats.org/drawingml/2006/picture&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">pic:nvPicPr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">pic:cNvPr </span><span style="color: #FF0000;">id</span><span style="color: #0000FF;">=&quot;0&quot;</span><span style="color: #FF0000;"> name</span><span style="color: #0000FF;">=&quot;Picture 1&quot;</span><span style="color: #FF0000;"> descr</span><span style="color: #0000FF;">=&quot;&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">pic:cNvPicPr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:picLocks </span><span style="color: #FF0000;">noChangeAspect</span><span style="color: #0000FF;">=&quot;1&quot;</span><span style="color: #FF0000;"> noChangeArrowheads</span><span style="color: #0000FF;">=&quot;1&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">pic:cNvPicPr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">pic:nvPicPr</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">pic:blipFill</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:blip </span><span style="color: #FF0000;">r:embed</span><span style="color: #0000FF;">=&quot;###imageid###&quot;</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:srcRect</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:stretch</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">a:fillRect</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                                        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">a:stretch</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                                    ...
            </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">root</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is the code that word generates for an image. Despite the complexities of the fragment, the important part is that the only things you need to change to include the image is substute the ###imageid### with the real id of the embedded object.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e22e11c4-8885-4ad4-ade9-0da9b4aeb750" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">String nodeContent </span><span style="color: #000000;">=</span><span style="color: #000000;">
    Properties.Resources.XmlContentForEmbedImage.Replace(
        </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">###imageid###</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">,
        document.MainDocumentPart.GetIdOfPart(newImage));</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to the method GetIdOfPart() of MainDocumentPart we are able to get the id of the previously embedded image, now we can simply insert this piece of Xml into the MainDocumentPart to include the image in the word document.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/OpenXml" rel="tag">OpenXml</a> </p>
<p><div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c0434554-e021-4c3e-8225-6802fe241955" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a target="_blank" href="http://technorati.com/tags/OpenXml" rel="tag">OpenXml</a></div>
</p>
<div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:bbd9fdde-e28e-45d6-9acd-10e551dcd4d5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><!--dotnetkickit--></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/09/02/openxml-format-insert-an-image-into-a-document/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OpenXml Office format, open and substitute text</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 15:27:16 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/</guid>
		<description><![CDATA[
			
				
			
		
Quite often, customers ask us to generate reports in word format, the main advantage of this approach is that people feels comfortable working with word, and they love the possibility to modify the report once generated. In the past years I used many techniques to reach this goal, but in the end, a lot of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F08%2F29%2Fopenxml-office-format-open-and-substitute-text%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F08%2F29%2Fopenxml-office-format-open-and-substitute-text%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Quite often, customers ask us to generate reports in word format, the main advantage of this approach is that people feels comfortable working with word, and they love the possibility to modify the report once generated. In the past years I used many techniques to reach this goal, but in the end, a lot of time ago I resort to write a simple <a href="http://www.codewrecks.com/blog/index.php/2008/07/09/generate-rtf-library-in-net/">RTF generator</a> that suites my needs.</p>
<p>In these days I reach the point where the complexities of the documents became really difficult to manage with RTF generator, moreover we need a software that permits the customers to create a master word document, then the software should only makes substitution in some prefixed part with the real data. The obvious solution seems to use Office automation, but we need this code in a web application, and office automation is not <a target="_blank" href="http://blogs.msdn.com/david.wang/archive/2006/05/11/Office-Automation-and-IIS.aspx">supported by microsoft</a> in non interactive environment&nbsp; . Moreover I used this technique in the past, and it is terribly slow for big documents.</p>
<p>We decided to create word 2007 document so I move to OpenXml format. <a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB-4A1D-4C52-BDB5-7DD7E816D046&amp;displaylang=en">Microsoft have a SDK</a> that permits you to manage this new format, it permits you only to work with the overall structure of the document, manage the unzipping, adding part and zipping again document, so it&#8217;s up to you to manage the XML to modify the document, but thanks to LINQ 2 XML we can really do this in a simple way. Let&#8217;s start with a little example. I&#8217;ve created a simple document with this content</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/08/image6.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="125" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/08/image-thumb7.png" width="644" border="0"></a>&nbsp;</p>
<p>My goal is to find the<strong> $$$substituteme(30)</strong> in the document, <em>change it with another text and save the new document with another name</em>. I create an helper class that does this for me, here is the constructor.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:64488670-0b0f-4abb-8670-aac5e1a2c9e6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> Document(String originalDocumentPath, String destinationDocumentPath)
{
    File.Copy(originalDocumentPath, destinationDocumentPath, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">);
    Doc </span><span style="color: #000000;">=</span><span style="color: #000000;"> WordprocessingDocument.Open(destinationDocumentPath, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The SDK does not permits me to save document with another name, so I simply copy the master file with the name I want, then open the copy; the overall effect is the same, mantaining the master unchanged and have a file with a given name. The WordprocessingDocument class is the root class that you should use to manage docx files, it is true that a docx file is simply a zip file, but this class shield you from this permitting you to browse file content. My class assign the document to a private propery called Doc that does some basic management.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ad002646-dc8f-4f69-bcc3-c2347fa1c6e7" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">private</span><span style="color: #000000;"> WordprocessingDocument Doc
{
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> doc; }
    </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (doc </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) doc.Dispose();
        doc </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;
        GrabDocumentParts();
    }
}

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> GrabDocumentParts()
{
    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (doc </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (StreamReader sr </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StreamReader(doc.MainDocumentPart.GetStream()))
        {
            </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (XmlReader xmlr </span><span style="color: #000000;">=</span><span style="color: #000000;"> XmlReader.Create(sr))
            {
                mainDocument </span><span style="color: #000000;">=</span><span style="color: #000000;"> XElement.Load(xmlr);
            }
        }
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>First of all I dispose a previous document if present (actually it is not needed because I can setup document only in the constructor), suddenly&nbsp; I read all the content of the MainDocumentPart into an <a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx">XElement</a> variable. Now I can change the XElement to substitute text.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9e0f9e70-547e-4597-87b3-fb0018d4fe73" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> XNamespace namespace_w </span><span style="color: #000000;">=</span><span style="color: #000000;"> XNamespace.Get(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">http://schemas.openxmlformats.org/wordprocessingml/2006/main</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Document Substitute(String tagToSearch, String textToSubstitute)
{
    XElement node </span><span style="color: #000000;">=</span><span style="color: #000000;"> FindNodeByTag( tagToSearch);
    node.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> node.Value.Replace(tagToSearch, textToSubstitute);
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">this</span><span style="color: #000000;">;
}

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> XElement FindNodeByTag(String tagToSearch)
{
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> (from b </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> mainDocument.Descendants(namespace_w </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">t</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)
            </span><span style="color: #0000FF;">where</span><span style="color: #000000;"> b.Value.Trim() </span><span style="color: #000000;">==</span><span style="color: #000000;"> tagToSubstitute
            select b).Single();
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The code is really simple, to find the code I search for a node named w:t; remember that you need to use XML namespaces to make queries. When I found the node I simply substitute the original text with the one I want and since I love fluent interfaces, the Substitute method return the original Document object to chain calls. Finally I need a Save method that close the document and save all modified content to the file.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:7816a2ee-293f-4a6c-aafa-c22c5e804ed9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Save()
{
    </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (Stream s </span><span style="color: #000000;">=</span><span style="color: #000000;"> doc.MainDocumentPart.GetStream(FileMode.Create, FileAccess.Write))
    {
        </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (XmlWriter xmlw </span><span style="color: #000000;">=</span><span style="color: #000000;"> XmlWriter.Create(s))
        {
            mainDocument.WriteTo(xmlw);
        }
    }
    doc.Close();
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The process is really simple, I call GetStream of the MainDocumentPart object, but with FileMode.Create that requires the creation of a new stream, then I simply write to the stream all the content of the modified XElement, and the game is done. Here is a typical use.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:2274deb3-036a-4796-8285-0a7b2777c044" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Document doc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Document(</span><span style="color: #800000;">@&quot;</span><span style="color: #800000;">samples\doc2.docx</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">@&quot;</span><span style="color: #800000;">samples\doc1saved.docx</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
doc.Substitute(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">$$$substituteme(30)</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">First Substitution!!!</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)
    .Substitute(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">$$$substituteme(25)</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Second Substitution!!!</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)
    .Save();</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to the fluent interface I can use simple syntax to change various part of the document. Working with OpenXml makes really easy to manage Word documents.</p>
<p>alk.</p>
<p><div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:591cb08d-5294-48c6-a405-76d51ed536b8" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a target="_blank" href="http://technorati.com/tags/OpenXml" rel="tag">OpenXml</a></div>
</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/OpenXml" rel="tag">OpenXml</a> </p>
<div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:6cf98d38-ee71-4fb6-90ce-7db072563c8a" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><!--dotnetkickit--></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/08/29/openxml-office-format-open-and-substitute-text/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
