Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:28:10+00:00 2026-06-18T06:28:10+00:00

Okay first off I would say I have next to no experience in using

  • 0

Okay first off I would say I have next to no experience in using COM references so just playing around with Microsoft.Office.Interop.Word so please keep that in mind if my code looks a little overblown. Essentially I want to get a string or object to be put into a database to parse or else run logic on the objects directly in C# and put parts of them into a database.

The essence of the problem is I am not sure using the library how you iterate to current form field in a given paragraph reference. Please examine code below and let me know if you have any suggestions. I think I just don’t know the proper property or method to give me an iteration to help me finish my code.

Basically I create a Word App, create Word Doc that references opening the Word App, I iterate through the paragraphs in the doc, I break up the blocks into character arrays. In my code when the ASCII character equivalent of 21 comes up I know that is a type of form box I am wanting to parse. However I can’t get the iteration to increment successfully which is strange, no matter where I set the scope of the int and the increment set it never changes scope. Thus I am at a loss and was curious if there was a better way to do what I was doing as well. There may be a simpler way to do what I am doing. I know for me to solve the issue I could potentially make a method to just return the Form objects seperate from paragraph objects. But that seems strange to me so I figured I would ask.

I am using .NET 4.5 and had to manually add the DLL for Microsoft.Office.Interop.Word ver 15 as the reference for some reason when we upgraded to Office 2013 did not update the references directly in VS. Here is my rather messy code:

public static string ReadTest(string loc)
        {
            Word._Application wordApp = new Word.Application();
            Word._Document Doc = wordApp.Documents.Open(loc, ReadOnly: true);

            try
            {
                sb = "";

                // This will get me JUST THE FORMS info
                //foreach (Word.FormField form in Doc.FormFields)
                //{
                //    sb += form.Result + "\n";
                //}


                int x = 1;

                foreach (Word.Paragraph objParagraph in Doc.Paragraphs)
                {
                    string st = "";

                    try
                    {
                        foreach (char c in objParagraph.Range.Text)
                        {
                            if (((int)c) != 21)
                            {
                                st += c;
                            }
                            else
                            {
                                st += Doc.FormFields.get_Item(x).Result;
                            }
                        }

                        sb += st + "\n";

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    x += 1;

                }

            }
            catch (COMException) { }
            finally
            {
                //FileInfo finfo = new FileInfo(loc);
                //finfo.IsReadOnly = false;

                if (Doc != null)
                {
                    Doc.Close();
                    Doc = null;
                }
                if (wordApp != null)
                {
                    wordApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
                    wordApp = null;
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            return sb;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:28:11+00:00Added an answer on June 18, 2026 at 6:28 am

    For sure the right position for the x increment is the line immediately after the one where you are accessing the form field:

                            else
                            {
                                st += Doc.FormFields.get_Item(x).Result;
                                x++;
                            }
    

    I don’t know if you already tried to put it there, but surely the code you posted could work only when your doc has exactly one form field for each paragraph.

    In fact, if there is more than one field, when you find the 2nd, 3rd, and so on 21 character x wouldn’t have been incremented yet, so you’d end up reading always the same field.

    If for instance there are only a field in the first paragraph, and another one in the third, your code would find the first one, then increment x and go on to read the second paragraph with x = 2 without finding any 21 char. Then x would be incremented once again and you would scan the characters of the third paragraph with x = 3 when there are only 2 fields, and so when you get the 21 char you look for a field (the third one) that doesn’t exist.

    PS It would be much simpler helping you working with a sample doc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm using XCode 4.0.2. Okay, here is my issue. I can build
first, I can't stand Crystal! Okay, that's off my chest... Now, we have an
Okay. I have completed my first python program.It has around 1000 lines of code.
OKay first off this is really really similiar to the http://dribbble.com homepage. In the
Okay so I have two problems. First off I have made a square that
First off, I would just like to mention my absolute hatred of Internet Explorer
Okay, allow me to say first off that I am working on developing my
First off I'm using Mac. Next, I need to execute this "file.sh" we will
Okay, first off, I am build a web app using AngularJS and node.js. I
Okay at first I thought this would be pretty straightforward. But I can't think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.