-
May 5th, 2013, 09:58 PM
#1
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.
-
May 6th, 2013, 10:34 PM
#2
Re: C# check merge field check box in a word document
Looking for help regarding this, cant find anything anywhere!
-
May 7th, 2013, 06:38 PM
#3
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?
-
May 7th, 2013, 06:46 PM
#4
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
-
May 7th, 2013, 07:50 PM
#5
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?
-
May 7th, 2013, 08:39 PM
#6
Re: C# check merge field check box in a word document
Compiling and running the application is debugging it ins't it?
-
May 8th, 2013, 01:37 PM
#7
Re: C# check merge field check box in a word document
Originally Posted by Dredious
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?
-
May 8th, 2013, 06:57 PM
#8
Re: C# check merge field check box in a word document
I'm compiling in the IDE with F5.
-
May 8th, 2013, 07:45 PM
#9
Re: C# check merge field check box in a word document
Originally Posted by Dredious
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?
-
May 8th, 2013, 09:07 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|