0

In my Word document are various document properties. When I open the word document with the Wordproccessing object and convert the document to HTML the fields aren´t replaced.

Word Doucment Properties

Word Document

How can I programmatically refresh these values?

my current Code is:

 using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(item.File.OpenBinary(), 0, item.File.OpenBinary().Length);

            OpenSettings os = new OpenSettings();

            using (WordprocessingDocument doc = WordprocessingDocument.Open(ms, true, os))
            {

                HtmlConverterSettings settings = new HtmlConverterSettings()
                {
                    PageTitle = "My Page Title"
                };
                XElement html = HtmlConverter.ConvertToHtml(doc, settings);

                Output.Text = html.ToStringNewLineOnAttributes();

            }
        }

I´m using OpenXMLPowerTools and DocumentFormat.OpenXML

In the HTML Output the fields appears as follows:

<span
      lang="de-DE"
      class="pt-Platzhaltertext">[Anrede]</span>

XML - Code in Word Doucment:

<w:sdt>
<w:sdtPr>
<w:alias w:val="Anrede"/>
<w:tag w:val="Anrede"/>
<w:id w:val="1584338639"/>
<w:placeholder>
    <w:docPart w:val="F2B19A5D640C4F3295F8F7C07F7DD0BD"/>
</w:placeholder>
<w:showingPlcHdr/>
<w:dataBinding w:prefixMappings="xmlns:ns0='http://schemas.microsoft.com/office/2006/metadata/properties' xmlns:ns1='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns2='http://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:ns3='e0f4b66e-a41b-4a2f-a5f4-37511d6ee82e' " w:xpath="/ns0:properties[1]/documentManagement[1]/ns3:Anrede[1]" w:storeItemID="{E0660C1E-777E-43B2-B0E8-59A22ED635AC}"/>
<w:text/></w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidRPr="00300F69">
    <w:rPr>
    <w:rStyle w:val="Platzhaltertext"/>
    </w:rPr>
    <w:t>[Anrede]</w:t>
</w:r>
</w:sdtContent>

YowE3K
  • 23,852
  • 7
  • 26
  • 40
  • Where are you calling `Save()`? – FortyTwo May 08 '17 at 07:33
  • Nowhere I don´t want to save the document. – WhatEver2337 May 08 '17 at 07:44
  • Is `` under settings set to true? [This](https://blogs.msdn.microsoft.com/pfedev/2010/08/08/openxml-how-to-refresh-a-field-when-the-document-is-opened/) might be helpful. – FortyTwo May 08 '17 at 07:53
  • Since you using a memory stream, calling `Save()` will flush the memory stream and won't save any changes to your original document – FortyTwo May 08 '17 at 07:56
  • There is no tag "updateFields" in the word document. (I added the XML - Code above) Maybe the class StdProperties is helpful but i don't know how to update it with the databinding. – WhatEver2337 May 08 '17 at 08:27
  • `UpdateFields` is present under `settings.xml` and it will automatically recalculate field values when the document is opened. [Read more here](https://msdn.microsoft.com/en-us/library/cc861799). – FortyTwo May 08 '17 at 08:42
  • If you want to replace text in your content control, i.e. the text in the field [Anrede] should be replaced, you can [look at this post](http://stackoverflow.com/questions/31750228/replacing-text-of-content-controls-in-openxml). Since you are loading your document into memory stream, calling `Save()` will update the stream but not make changes to your original document. – FortyTwo May 08 '17 at 08:45
  • I have called Save() but there are no changes in the HTML Output. Also I don't want to replace the Paragraph, I only want to update die "sdtContet" with the data from the databinding. Look at the screenshots, there are various "Document Properties" and i want to refresh them in the word document, without open MS Word. Example: The Word document ist copied from a template and so only the values in "CustomXML\item2.xml" are changed but not the values in "\word\document.xml" – WhatEver2337 May 08 '17 at 09:01
  • This is how you should save `//If xPart is your custom xml part` `var stream = xPart.GetStream(FileMode.Create);` `Save(stream);` – FortyTwo May 08 '17 at 09:25
  • Are you making changes in the word document which need to reflect in your HTML app or the other way around? – FortyTwo May 08 '17 at 09:27

0 Answers0