<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx</link><description>With the release of Microsoft Office 2007 we can work with a much simpler, standard, XML format called Open XML which opens the door for many types of applications that cannot work via COM. What if you needed to build a scalable web service that processes</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#10152535</link><pubDate>Tue, 12 Apr 2011 13:43:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10152535</guid><dc:creator>MAP</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;this article is good and i am thankful to you.&lt;/p&gt;
&lt;p&gt;but some problem i am having that... URI path is not working....&lt;/p&gt;
&lt;p&gt;can u please suggest me.....&lt;/p&gt;
&lt;p&gt;this part&lt;/p&gt;
&lt;p&gt;Dim uri As New Uri(&amp;quot;/word/document.xml&amp;quot;, UriKind.Relative)&lt;/p&gt;
&lt;p&gt;this URI not getting any path value....&lt;/p&gt;
&lt;p&gt;and no styles and footer added to the newly created doc file!&lt;/p&gt;
&lt;p&gt;Please help&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Map.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10152535" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#10118071</link><pubDate>Thu, 20 Jan 2011 10:45:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10118071</guid><dc:creator>Reddy Lutonadio</dc:creator><description>&lt;p&gt;Great blog!&lt;/p&gt;
&lt;p&gt;I have made &amp;nbsp;some modifications. Instead of adding the following&lt;/p&gt;
&lt;p&gt;&amp;lt;w:p w:rsidR=&amp;quot;00622A50&amp;quot; w:rsidRDefault=&amp;quot;00622A50&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;w:r&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;w:br w:type=&amp;quot;page&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/w:r&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/w:p&amp;gt;&lt;/p&gt;
&lt;p&gt;for the break page in the query,&lt;/p&gt;
&lt;p&gt;I have added it in the for each statement, like this&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;For Each letter In letters&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim nextbody = letter.Documents...&amp;lt;w:body&amp;gt;.First&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If i = 0 Then&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainBody.ReplaceNodes(nextbody.Elements)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;#39;Add a page break if there is another letter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainBody.Add(&amp;lt;w:p w:rsidR=&amp;quot;00622A50&amp;quot; w:rsidRDefault=&amp;quot;00622A50&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;w:r&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;w:br w:type=&amp;quot;page&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/w:r&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/w:p&amp;gt;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainBody.Add(nextbody.Elements)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;i += 1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Next&lt;/p&gt;
&lt;p&gt;This way, a blank page is not printed after all other pages have been printed.&lt;/p&gt;
&lt;p&gt;Good or bad? Is there a better way of doing it?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10118071" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9959063</link><pubDate>Fri, 05 Feb 2010 23:09:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9959063</guid><dc:creator>PhilM</dc:creator><description>&lt;p&gt;Yeah, the next move would be to figure out how to use XSLT to replace the placeholders with the appropriate data. Then I wouldn't have to go to string, use replace, and then go back. And I probably would not have to import System.Web (I was only looking for HTMLEncode because some of the data has special characters).&lt;/p&gt;
&lt;p&gt;The Client Profile is new to me but I'm going to run all of this server-side where presumably the full .Net Framework will be installed.&lt;/p&gt;
&lt;p&gt;Thanks for your help. Anything I can do to say thanks? philm@cottagecomputing.com&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9959063" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958984</link><pubDate>Fri, 05 Feb 2010 20:13:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958984</guid><dc:creator>Beth Massi</dc:creator><description>&lt;p&gt;Hi PhilM,&lt;/p&gt;
&lt;p&gt;Great, you could also use XSLT as well this way. Just be careful with your Replace statements because it may give you unpredictable results. If you have a placeholder name that the user is also using in the template itself it will overwrite it. &lt;/p&gt;
&lt;p&gt;Also by using System.Web you won't be able to run on the Client Profile (but that probably doesn't matter for your case). &lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;-B&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958984" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958602</link><pubDate>Fri, 05 Feb 2010 03:36:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958602</guid><dc:creator>PhilM</dc:creator><description>&lt;p&gt;Hey, I found an archaic way to do it!! I can now load the XML from a template docx, loop through all the orders creating letters with the data from the order in each one. It uses your code but drops the XML Literal and doesn't use LINQ. (Sorry, but LINQ seems to be great for compile time).&lt;/p&gt;
&lt;p&gt;This may be self-evident:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Sub Main()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Mail merging....&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim db As New NorthwindDataContext&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Letter query -- Create a query that will create Letter objects for each&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;' of the customers in Northwind that have shipping orders:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'My version&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim strLetter As String&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim strLetter2 As String&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Now load it dynamically&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim sourceFile = CurDir() &amp;amp; &amp;quot;\QuickTest.docx&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Open the Document template using the Open XML SDK&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Using wordDoc = WordprocessingDocument.Open(sourceFile, False)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'get the main document part (document.xml)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim mainPart = wordDoc.MainDocumentPart()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim mainDocumentXML As XDocument&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Using sr As New StreamReader(mainPart.GetStream)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainDocumentXML = XDocument.Load(sr) 'load the document XML&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Prepend the document XML. StrLetter becomes our defacto template for each Letter.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;strLetter = &amp;quot;&amp;lt;?xml version=&amp;quot;&amp;quot;1.0&amp;quot;&amp;quot; encoding=&amp;quot;&amp;quot;UTF-8&amp;quot;&amp;quot; standalone=&amp;quot;&amp;quot;yes&amp;quot;&amp;quot;?&amp;gt;&amp;quot; + mainDocumentXML.ToString&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim MyLetters As New List(Of Letter)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim MyLetter As Letter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;For Each Order In db.Orders 'No filters to make it simple&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyLetter = New Letter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;strLetter2 = strLetter.Replace(&amp;quot;CUSTOMERNAME&amp;quot;, HtmlEncode(Order.Customer.CompanyName)) 'Must Import System.Web.Httputility&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyLetter.CustomerID = Order.Customer.CustomerID&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyLetter.Document = XDocument.Parse(strLetter2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyLetters.Add(MyLetter)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Next&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CreateOneLetter(MyLetters)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958602" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958569</link><pubDate>Fri, 05 Feb 2010 01:55:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958569</guid><dc:creator>PhilM</dc:creator><description>&lt;p&gt;Yeah, I'm actually headed for a server side solution with no MSOffice Installed. So that's why the VSTO solution is out.&lt;/p&gt;
&lt;p&gt;Thanks, I'll check often to see if you have new ideas. What I'll do for now is recompile when the user changes the letter layout (or anything about it). There must be a way..... Thanks again&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958569" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958549</link><pubDate>Fri, 05 Feb 2010 01:15:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958549</guid><dc:creator>Beth Massi</dc:creator><description>&lt;p&gt;You could definitely do this with a VSTO add-in or template. That way you could give the user the ability to drag placeholders onto the actual letter and then you could just use VSTO to query the data and populate the document directly. &lt;/p&gt;
&lt;p&gt;I'm assuming you don't like this idea though because you're trying to automate and scale the generation process. But VSTO is a great end-user solution.&lt;/p&gt;
&lt;p&gt;I'll think about your scenario, I think there's a way I just need to focus on it for a bit (and I have a million things running around attm ;-))&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958549" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958527</link><pubDate>Fri, 05 Feb 2010 00:40:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958527</guid><dc:creator>PhilM</dc:creator><description>&lt;p&gt;Thanks, you've been quite helpful. This is close to being another dead end I guess. At least I learned how it worked. I thought when I saw that you hoisted the XML out of the Word template, put it in the program and modified it, that I could do the same at run time. That way the user could modify the layout of the template without a recompile of the program. No soap I guess. Now what, I'll look over your 'reverse' idea but I really want to have the user have control of the template yet still be able to run a merge using it. There's gotta be a way... sigh.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958527" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958391</link><pubDate>Thu, 04 Feb 2010 19:54:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958391</guid><dc:creator>Beth Massi</dc:creator><description>&lt;p&gt;Hi PhilM,&lt;/p&gt;
&lt;p&gt;You need to realize that the use of embedded expressions is ONLY going to work if you are doing it in the VB editor because these expressions are compiled just like any other code. It's not script. Meaning that if you have &amp;lt;%= %&amp;gt; in your XML &amp;quot;string&amp;quot; these will not be compiled magically by the Parse method, you'll end up with invalid XML. Does that make sense?&lt;/p&gt;
&lt;p&gt;Maybe you could try to add your own attributes to the WordProcessingML? I'll try to play with this when I have some free time. &lt;/p&gt;
&lt;p&gt;You may want to think about it in reverse. You can use embedded expressions to actually create the XML itself, not just the values. That way you could construct the template on the fly. i.e. you could do &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim myElementName = &amp;quot;MyElement&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim myValue = &amp;quot;Value&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim x = &amp;lt;&amp;lt;%= myElementName %&amp;gt;&amp;gt;&amp;lt;%= myValue %&amp;gt;&amp;lt;/&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(x.ToString)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.ReadLine()&lt;/p&gt;
&lt;p&gt;Not sure if that helps you but it's a thought. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958391" width="1" height="1"&gt;</description></item><item><title>re: Mail Merging Into a Single Word Document Using XML Literals</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/09/23/mail-merging-into-a-single-word-document-using-xml-literals.aspx#9958372</link><pubDate>Thu, 04 Feb 2010 19:25:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9958372</guid><dc:creator>PhilM</dc:creator><description>&lt;p&gt;There doesn't seem to be any rational way to have repeated data bind to the same content control (e.g. Content control for customer name in the template appears once, but needs to repeatedly bound to new custom xml elements as in a loop. One only has to bing 'repeating content controls' to find out how big a problem this is.&lt;/p&gt;
&lt;p&gt;I've played a lot with the OpenXML SDK and it offers nothing for this either. At least not that I have found.&lt;/p&gt;
&lt;p&gt;Your code is SO CLOSE!!! I want to change the last statement in the linq SELECT to use a variable which I have loaded from the template, and modified to insert the &amp;lt;%=CustomerName variable that you showed. But my documents are coming out invalid in Word, not sure why. My code follows (from Module 1) where in I repalced the XML literal with a variable that I can't seem to get right:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Using sr As New StreamReader(mainPart.GetStream)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainDocumentXML = XDocument.Load(sr) 'Note change from Xelement&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim mainDocumentXMLString As String = mainDocumentXML.ToString&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Perform magic (Please the placeholders with the &amp;lt;% = variables&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mainDocumentXML = XDocument.Parse(mainDocumentXMLString)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;'Letter query -- Create a query that will create Letter objects for each&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;' of the customers in Northwind that have shipping orders:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim letters = _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;From Order In db.Orders _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Join Employee In db.Employees _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; On Order.EmployeeID Equals Employee.EmployeeID _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Where Order.OrderDate IsNot Nothing AndAlso _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Order.ShippedDate IsNot Nothing AndAlso _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Order.ShippedDate.Value.Date &amp;gt;= #1/1/1998# _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Let DateOrder = Order.OrderDate.Value.ToShortDateString _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Let DateShip = Order.ShippedDate.Value.ToShortDateString _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Select New Letter With { _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .CustomerID = Order.CustomerID, _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .OrderID = Order.OrderID, _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .EmployeePhoto = If(Employee.Photo IsNot Nothing, Employee.Photo.ToArray, Nothing), _&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .Document = mainDocumentXML}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9958372" width="1" height="1"&gt;</description></item></channel></rss>