tags:

views:

20

answers:

1

In relation to this post I've been working heavily with Word's Open XML and the libraries available. What seems to be unknown to most is that there is some great resources out there for programatically dealing with the Open XML format for Word (and I don't mean manually writing out XML to the filestream!).

My question is regarding something I'm doing. Basically, I'm manually creating a mail merge. Now, the mail merge actually works fine, what doesn't work fine, however, is placing the mail merge fields within the document. This is what I'm doing:

string mergeFieldName = m.Value.Replace("[", string.Empty).Replace("]", string.Empty);
Body body = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
SimpleField simpleField = new SimpleField() { Instruction = " MERGEFIELD " + mergeFieldName + " " };
Run run = new Run();
RunProperties runProperties = new RunProperties();
NoProof noProof = new NoProof();
runProperties.Append(noProof);
Text text = new Text("«" + mergeFieldName + "»");
run.Append(runProperties);
run.Append(text);
simpleField.Append(run);
p.Append(simpleField);
body.Append(p);

docXml.InnerXml = docXml.InnerXml.Replace(m.Value, body.InnerXml);

The variable m is actually a Match object based on my regular expression matching everything within square brackets. (You might be wondering why I'm doing this like I am - that's not relevant here)

My problem is the resulting XML is invalid because I have the following:

<w:p w:rsidR="00945DC1" w:rsidRDefault="006878CA">
  <w:r>
    <w:t xml:space="preserve">
      <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;
        <w:fldSimple w:instr=" MERGEFIELD FullName ">
          <w:r>
            <w:rPr>
              <w:noProof />
            </w:rPr>
            <w:t>«FullName»</w:t>
          </w:r>
        </w:fldSimple>
      </w:p>
    </w:t>
  </w:r>
</w:p>

And here's an example of the working XML from a manually created document:

<w:p w:rsidR="001B2242" w:rsidRDefault="001B2242">
  <w:r>
    <w:tab/>
  </w:r>
  <w:fldSimple w:instr=" MERGEFIELD &quot;SalutationName&quot; ">
    <w:r>
      <w:rPr>
        <w:noProof/>
      </w:rPr>
      <w:t>«SalutationName»</w:t>
    </w:r>
  </w:fldSimple>
</w:p>

The actual error I get is "The file cannot be opened because there are problems with the contents". If I don't write the mail merge fields out, load the document, and then insert mail merge fields, it works. So the actual mail merge is correct, it's just the mail merge fields that aren't correct. (See my referenced post for how to get the mail merge out).

Any ideas on this one?

Cheers

A: 

What I see is that you are replacing it wrong. You are putting the new XML you are creating inside a w:t tag which is a text tag If i'm not mistaken'

Skip the Body creation and replace the InnerXml of the Paragraph p with the innerXml of m.Values parent.

That is if I'm understanding this correctly.

Ingó Vals