Solved

Office.Interop Word 2010 FormFields Iteration

Posted on 2010-11-26
9
1,548 Views
Last Modified: 2012-08-13
Using these directions...

http://office.microsoft.com/en-us/word-help/create-forms-that-users-complete-in-word-HA010030746.aspx

I have a Word 2010 docx form I created using the developer tab in Word. I added a textbox, buildingblock control, DropDown List Control. I saved the document.


using Word = Microsoft.Office.Interop.Word;
            var wordApp = new Word.Application();
            wordApp.Visible = true;
            Word.Documents docs = wordApp.Documents;
            Word.Document doc = docs.Open(@"c:\temp\test3.docx", Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Word.Range range = doc.Range(0, 0);


            Word.FormFields fields = doc.FormFields;
            foreach (Word.FormField field in fields)
                MessageBox.Show(field.Name + " " + field.Result + "\n");


            doc.Close(Type.Missing, Type.Missing, Type.Missing);
            wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);


The issue is that the C# loop doesn't hit any fields. I don't understand why not. I have tried
references:
Microsoft.Office.Interop.Word Version 12.0.0.0
Microsoft.Office.Interop.Word Version 14.0.0.0

Can you see my mistake?

I am using Windows 7 Ultimate x64.

Thanks,

Geoff
test3.docx
0
Comment
Question by:gswitz
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
Try it.......

object fname = @"c:\temp\test3.docx";
Word.Document doc = docs.Open(ref fname, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

follow:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26629764.html
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
object fname = @"c:\temp\test3.docx";
            object missing = System.Reflection.Missing.Value;
            object ReadOnly = true;

            Word.Document doc = docs.Open(ref fname, ref missing,
                                          ref ReadOnly, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing);
0
 

Author Comment

by:gswitz
Comment Utility
I copied this in verbatim and my
foreach (Word.FormField field in fields)
loop still didn't find any fields.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
See the code. This may help you to proceed.
var wordApp = new Word.Application();

            wordApp.Visible = true;

            Word.Documents docs = wordApp.Documents;

            object fname = @"c:\temp\test3.docx";

            object missing = System.Reflection.Missing.Value;

            object ReadOnly = true;

           // object isVisible = true;

            Word.Document doc = null;

            try

            {

                

     

           

                doc = docs.Open(ref fname, ref missing,

                                              ref ReadOnly, ref missing, ref missing,

                                              ref missing, ref missing, ref missing,

                                              ref missing, ref missing, ref missing,

                                              ref missing, ref missing, ref missing,

                                              ref missing, ref missing);          





                //doc.Activate();



                Console.WriteLine("No Of tables=" + doc.Tables.Count);

                Word.Range rng;

                foreach (Word.Table tbl in doc.Tables)

                {

                    int cl = tbl.Columns.Count;

                    int rows = tbl.Rows.Count;

                    Console.WriteLine("No of cols = " + cl + " Rows = " + rows);

                    for (int i = 1; i <= rows; ++i)

                    {

                        for (int c = 1; c <= cl; c++)

                        {



                            rng = tbl.Cell(1, 1).Range;

                            rng.SetRange(tbl.Rows[i].Cells[c].Range.Start,

                                tbl.Rows[i].Cells[c].Range.End - 1);

                            Console.Write(rng.Text); Console.Write(" ");

                        }

                        Console.WriteLine();



                    }



                }



         

                



            }

            catch (Exception ee)

            {

                Console.WriteLine("Error!" + ee.Message);

            }



            finally

            {

                if(doc != null)

                    doc.Close(ref missing, ref missing, ref missing);

                if (wordApp != null)

                wordApp.Quit(ref missing, ref missing, ref missing);

            }

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 500 total points
Comment Utility
There are no form fields in your document. There is only a two-column, four-row table with text in each cell.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
Oops. I missed the fact that there are Content controls. This is how to list them in VB. Sorry I don't have C#.


Dim cc As ContentControl

For Each cc In ActiveDocument.ContentControls

    MsgBox cc.Range.Text

Next cc

Open in new window

0
 

Author Comment

by:gswitz
Comment Utility
Great, Graham! This gets me  very close. Could you show me how to change the value in the Gender control to Female?

Thanks!
0
 

Author Comment

by:gswitz
Comment Utility
0
 

Author Comment

by:gswitz
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Printing A1 on A4 Printer 8 54
C# Update Status Bar.... 1 22
bulid json format 3 15
In MS Word, rename styles in a batch 9 13
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now