none
MERGEFIELD Field Code Issues RRS feed

  • Question

  • We have created a document template (*.doct) which outlines several MERGEFIELD's which were created using the wizard within Word 2013.
    The purpose of the application is that we are trying to create invoices by passing data to a document template where it will use the MERGEFIELD's to populate the invoice with the wanted data.
    We are basing our logic from this sample:
    http://www.jarredcapellman.com/2012/10/22/c-openxml-mail-merge-complete-example/
     
    When doing a lookup of the MERGEFIELD's being used, we are receiving nonsensical field codes (e.g. The field codes are being cut into two lines. The field codes that we are using do not include any white space)
    It appears that the parsing is being done incorrectly.
    If anyone can make any sense of this that would be great.


    Monday, February 10, 2014 4:47 PM

All replies

  • Hi,

    The code provided by the link ran successfully without the field codes being cut into two lines. I also create a simple sample based on the code. Below is the steps:
    1. Create a word document
    2. Start mail merge wizard and insert the merge filed like figure below

    3. add the code like below to get simple field

    // get simple field
                        foreach (SimpleField field in docGenerated.MainDocumentPart.RootElement.Descendants<SimpleField>())
                        {                                                
                            var fieldname = field.GetAttribute("instr", field.NamespaceUri).Value.Replace("MERGEFIELD", "").Trim();
                            var fieldValue = GetMergeValue(FieldName: fieldname);
    
                            // Go through all of the Run elements and replace the Text Elements Text Property
                            foreach (Run run in docGenerated.MainDocumentPart.Document.Descendants<Run>())
                            {
                                foreach (Text txtFromRun in run.Descendants<Text>().Where(a => a.Text == "«" + fieldname + "»"))
                                {
                                    txtFromRun.Text = fieldValue;
                                }
                            }
                        }
      private string GetMergeValue(string FieldName) {
                switch (FieldName) {
                    case "CurrentDate":
                        return DateTime.Now.ToShortDateString();
                    case "CPU_Count":
                        return Environment.ProcessorCount.ToString();
                    case "First":
                        return "Jack";
                    case "Company":
                        return "Microsoft";
                    default:
                        throw new Exception(message: "FieldName (" + FieldName + ") was not found");
                }
            }
    

    The code also works well, would you mind sharing some code snippet with us and describing the detail steps to reproduce the issue.

    Best regards
    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 11, 2014 12:01 PM
    Moderator
  • Hi Ben

    <<We have created a document template (*.doct)...>>

    FWIW Word should NOT be opening an Open XML file with such an extension. Your file name needs to be *.dotx or *.dotm (if it would contain macros).


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, February 12, 2014 7:21 PM
    Moderator