CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    May 2013
    Posts
    6

    C# check merge field check box in a word document

    Using Visual Studio 2012 with Office 2010.

    Just wondering how to check a check box content control on a word document, i have my C# Window Forms application filling in Merge Fields with data obtained from the forms such as textBoxes and comboBoxes. Though i was wondering how to check a text box from with in C#?

    I have been trying to find this for awhile now, and have resulted in actually asking the question here.

    The code below is how i connect to my word document and fill out the Merge Field on that document.


    Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;
    Word.Application oWord = new Word.Application();
    Word.Document oWordDoc = new Word.Document();

    oWord.Visible = true;

    if (File.Exists("pathName.txt"))
    {
    FileStream file = new FileStream("pathName.txt", FileMode.Open, FileAccess.Read);
    StreamReader Reader = new StreamReader(file);
    PathName = Reader.ReadLine();
    }

    Object oTemplatePath = PathName;

    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    foreach (Word.Field myMergeField in oWordDoc.Fields)
    {
    iTotalFields++;
    Word.Range rngFieldCode = myMergeField.Code;
    String fieldText = rngFieldCode.Text;

    if (fieldText.StartsWith(" MERGEFIELD"))
    {

    Int32 endMerge = fieldText.IndexOf("\\");
    Int32 fieldNameLength = fieldText.Length - endMerge;
    String fieldName = fieldText.Substring(11, endMerge - 11);

    fieldName = fieldName.Trim();



    if (fieldName == "Name")
    {
    myMergeField.Select();
    oWord.Selection.TypeText(firstName.Text);
    }

    if (fieldName == "Name2")
    {
    myMergeField.Select();
    oWord.Selection.TypeText(textBox6.Text);
    }

    if (fieldName == "LastName1")
    {
    myMergeField.Select();
    oWord.Selection.TypeText(textBox3.Text);
    }
    }

    Any help would be appreciated.

    Thank you.
    Last edited by Dredious; May 6th, 2013 at 11:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured