<?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>Calle Arnesten&#039;s Blog &#187; Office Open XML</title>
	<atom:link href="http://www.arnesten.com/category/office-open-xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arnesten.com</link>
	<description>Web Development, Test Driven Development, NHibernate, ASP.NET MVC, jQuery and more..</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Using Word automation to embed linked images</title>
		<link>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/</link>
		<comments>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/#comments</comments>
		<pubDate>Thu, 10 May 2007 17:41:52 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Office Open XML]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=4</guid>
		<description><![CDATA[In my current project I generate reports by creating Word 2007 files. You know the file format consisting of zipped XML files? This project is web-based and the users can enter information through rich text editors. These rich text editors produce html which in turn is inserted in the reports (with the altChunk tag for [...]]]></description>
			<content:encoded><![CDATA[<p>In my current project I generate reports by creating Word 2007 files. You know the file format consisting of zipped XML files? This project is web-based and the users can enter information through rich text editors. These rich text editors produce html which in turn is inserted in the reports (with the altChunk tag for those who are interested) . All this can be done without any Word automation which is good (having Office on the server is not supported by Microsoft).</p>
<p>Before today, only textual content with formatting could be entered through these text editors. But today I added the ability to upload and insert images as well. And here the problem started. When generating the reports the images would only be linked to and not embedded inside which is not what I wanted&#8230;</p>
<p>So how did I solve this? I cheated! Before today we were using Word automation for converting Word 2007 documents to Word 97 (using a web service) for backwards compatability. I decided to try and modify this web service to automatically embed linked images. I found an <a href="http://west-wind.com/weblog/posts/1178.aspx" target="_blank">article </a>by Rick Strahl which led me to the solution. Besides the things the article mentionend I also needed to perform the same thing with InlineShapes. Here comes the code:</p>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> EmbedLinkedImages(Document doc)
{
    <span class="kwrd">foreach</span> (InlineShape shape <span class="kwrd">in</span> doc.InlineShapes)
    {
        <span class="kwrd">if</span> (shape.LinkFormat != <span class="kwrd">null</span>)
        {
            shape.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            shape.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }

    <span class="kwrd">foreach</span> (Field field <span class="kwrd">in</span> doc.Fields)
    {
        <span class="kwrd">if</span> (field.Type == WdFieldType.wdFieldIncludePicture)
        {
            field.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            field.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }

    <span class="kwrd">foreach</span> (Shape shape <span class="kwrd">in</span> doc.Shapes)
    {
        <span class="kwrd">if</span> (shape.LinkFormat != <span class="kwrd">null</span>)
        {
            shape.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            shape.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }
}</pre>
<p>The code is mostly self-explanatory except the doc.UndoClear(). It is there to clear the undo buffer in Word which otherwise can consume a lot of memory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

