CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  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.

  2. #2
    Join Date
    May 2013
    Posts
    6

    Re: C# check merge field check box in a word document

    Looking for help regarding this, cant find anything anywhere!

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# check merge field check box in a word document

    I would start by dumping the fields to make sure they contain the word MERGEFIELD.

    Code:
    foreach (Word.Field myMergeField in oWordDoc.Fields)
    {
      Console.WriteLine(myMergeField);
    }
    Also, I notice you are search for the word MERGEFIELD with a space in front of it (e.g. " MERGEFIELD"). Are you sure it always appears with a space?

  4. #4
    Join Date
    May 2013
    Posts
    6

    Re: C# check merge field check box in a word document

    Thank you for your reply.

    Well the application works, with the space in front of the MERGEFIELD i have no issues with my textfields and comboBoxes filling in the mergefields.

    I just have Check Box Content Controls on the word document that i wish to Check based on what is Checked in my application.

    Thanks

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# check merge field check box in a word document

    Have you debugged your code in a debugger to make sure it works as you expect?

  6. #6
    Join Date
    May 2013
    Posts
    6

    Re: C# check merge field check box in a word document

    Compiling and running the application is debugging it ins't it?

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# check merge field check box in a word document

    Quote Originally Posted by Dredious View Post
    Compiling and running the application is debugging it ins't it?
    If you are talking about running the application in the IDE (with F5), setting breakpoints, inspecting variable values and so on, then yes. Otherwise, just running the application isn't debugging. Which are you doing?

  8. #8
    Join Date
    May 2013
    Posts
    6

    Re: C# check merge field check box in a word document

    I'm compiling in the IDE with F5.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# check merge field check box in a word document

    Quote Originally Posted by Dredious View Post
    I'm compiling in the IDE with F5.
    With regard to

    I just have Check Box Content Controls on the word document that I wish to Check based on what is Checked in my application.
    While debugging the program, have you looked at how to identify the check box content controls in code?

    Have you walked through the code to make sure the program is flowing the way you expect?

  10. #10
    Join Date
    May 2013
    Posts
    6

    Re: C# check merge field check box in a word document

    I ended up working it out with this, oWordDoc.ContentControls[index].Checked = true or false;

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