1

I would like to create list of mergefield which are inside Word template. After asking goole i find and use that code:

static void Main(string[] args)
{
    //OBJECT OF MISSING "NULL VALUE"
    Object oMissing = System.Reflection.Missing.Value;
    Object oTemplatePath = "D:\\MyTemplate.dotx";

    Application wordApp = new Application();
    Document wordDoc = new Document();

    wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    foreach (Field myMergeField in wordDoc.Fields)
    {
        Range rngFieldCode = myMergeField.Code;
        String fieldText = rngFieldCode.Text;

        // ONLY GETTING THE MAILMERGE FIELDS

        if (fieldText.StartsWith(" MERGEFIELD"))
        {
            // THE TEXT COMES IN THE FORMAT OF
            // MERGEFIELD  MyFieldName  \\* MERGEFORMAT
            // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

            Int32 endMerge = fieldText.IndexOf("\\");
            Int32 fieldNameLength = fieldText.Length - endMerge;

            String fieldName = fieldText.Substring(11, endMerge - 11);
            // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

            fieldName = fieldName.Trim();

            // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
            // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

            if (fieldName == "Name")
            {
                myMergeField.Select();
                wordApp.Selection.TypeText("Vivek");
            }
        }
    }
    wordDoc.SaveAs("myfile.doc");
    wordApp.Documents.Open("myFile.doc");
    wordApp.Application.Quit();
}

This work when Template consist <> mergefield, ut when mergefield is inside Textbox i isnt work. Can you tell me how scan whole document? or Textboxes too?

JoriO
  • 1,050
  • 6
  • 13
mikroice90
  • 137
  • 1
  • 9
  • Problem Solved, I needed to search all shapes in oWordDoc.Shape and check if msoType.msoTextBox exist if YES, use function like up. – mikroice90 Oct 23 '14 at 08:12

0 Answers0