none
Mail Merge field with formated value cannot be replaced with same formmated RRS feed

  • Question

  • HHow to replace the below xml with value "current date" and that should be return the result with the formatted value mentioned "dddd, MMMM dd, yyyy".

    <w:fldSimple w:instr="MERGEFIELD CurrentDate \@ "dddd, MMMM dd, yyyy"">

    - <w:r w:rsidR="00963793">
    - <w:rPr>
      <w:noProof />
      </w:rPr>
      <w:t>«CurrentDate»</w:t>
      </w:r>
      </w:fldSimple>

    </w:p>

    I have replaced the value for current date by using the code.But the current date is not displayed in the format "dddd, MMMM dd, yyyy". 

      if (fldSimples != null)
      {
              for (int i = (fldSimples.Length) - 1; i >= 0; i--)
               {
                   SimpleField fldSimple = fldSimples[i];
                   fldContent = fldSimple.InnerText.ToString();
                   fldContent = fldContent.Substring(1, fldContent.Length - 2);
                   xmlvalue = XMLdoc.SelectSingleNode(@"//LETTERDATA/" + fldContent);
                   fldContent = xmlvalue.FirstChild.InnerText;
                   fieldList += fldContent;
                                 
                   Run r = (Run)fldSimple.Elements<Run>().FirstOrDefault().CloneNode(true);
                   r.Descendants<Text>().FirstOrDefault().Text = fldContent;
                   fldSimple.PreviousSibling().Append(r);
                   fldSimple.RemoveAllChildren();
                   fldSimple.Remove();
               }
          }

    • Moved by George123345 Thursday, December 12, 2013 9:14 AM open xml issue
    Thursday, December 12, 2013 6:13 AM

Answers

All replies

  • Hi pradeep

    This message doesn't contain enough information about exactly what string you're replacing the mergefield with. As a moderator, I can see another message you posted and later deleted where you show an example of such information.

    Please note that when you place a string into the document, that's exactly what you'll get. There's nothing that's going to convert a data representation such as 22nd November 2013 to 22 November 2013. Your code would have to remove the "nd".


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, December 12, 2013 5:37 PM
    Moderator
  • I have created a template(.dotx) with mailmerge fields having format fields.By using open xml i have extracts the merge field using pacakage and replace the value form diferent xml file.the value has been replaced and the document will open with the newly repalced value.But the formated field value is not working properly.Please inform any better way to handle the format field to replace mailmerge fields.

    eg)

    My template have the current date field with the format

    {MERGEFIELD  CurrentDate /@ "dddd, MMMM dd, yyyy "}

    I have replaced the value of currentdate with "13/12/2013" from a xml file .But the 
    result was displaying as "13/12/2013" instead of "Friday, December 12, 2013".How to overcome this problem 

    When i extract the document main part Xml was dispalying like this for current date field.

    <w:fldSimple w:instr="MERGEFIELD CurrentDate \@ "dddd, MMMM dd, yyyy"">

    - <w:r w:rsidR="00963793">
    - <w:rPr>
      <w:noProof />
      </w:rPr>
      <w:t>«CurrentDate»</w:t>
      </w:r>
      </w:fldSimple>
    </w:p>

    I have replaced <w:t>«CurrentDate»</w:t> («CurrentDate») with  different Xml with the value (13/12/2013).

    str contain the current date


    nodes = TMPdoc.SelectNodes("//w:t[starts-with(text(),'«')]", nsManager);

       foreach (System.Xml.XmlNode node in nodes)
        {
            string str = node.FirstChild.InnerText;
            str = str.Substring(1, str.Length - 2);
            xmlvalue = XMLdoc.SelectSingleNode(@"//LETTERDATA/" + str);
          if (xmlvalue != null)
            {
                string result = xmlvalue.FirstChild.InnerText;
                node.FirstChild.InnerText = result.ToString();
            }
        }

    The value was replacing as 13/12/2013.But the template has the format form current date and the result to displayed as "Friday, December 12, 2013".



    Friday, December 13, 2013 6:18 AM
  • Hi pradeep

    The answer remains the same: YOUR CODE has to interpret the value and write out the date. In this case, I believe there are classes in the .NET Framework that can convert date information in a string to other date formats. See for example:

    http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
    http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
    http://msdn.microsoft.com/en-us/library/hc4ky857(v=vs.85).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 13, 2013 10:11 AM
    Moderator