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 7002539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:56:28+00:00 2026-05-27T20:56:28+00:00

Presently I am attempting to build a tool that will open a Microsoft Word

  • 0

Presently I am attempting to build a tool that will open a Microsoft Word document file and update all the fields in the document. Here is the main code:

using Microsoft.Office.Interop.Word;

public class clsDocumentFieldUpdateTool
{
    private static Microsoft.Office.Interop.Word.Application wordApp = null;
    private static Microsoft.Office.Interop.Word.Document oDoc = null;
    private static object missing = null;
    private static object readOnly = false;
    private static object visible = true;

    public static void OpenDocument(string docFileNameWithPath)
    {
        wordApp = new Microsoft.Office.Interop.Word.Application();
        missing = System.Reflection.Missing.Value;
        object fileToOpen = docFileNameWithPath;
        try
        {
            oDoc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref                     missing, ref visible, ref visible, ref missing, ref missing, ref missing);
        }
        catch (Exception excOpenFile)
        {
            MessageBox.Show(excOpenFile.Message +  System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excOpenFile.StackTrace);
        }
    }

    private static void Update(string file)
    {
        object nothing = System.Reflection.Missing.Value; // our 'void' value
        object filename = file; // our word template
        object notTrue = false;  // our boolean false

        try
        {
            //
            // now we want to load the template and check how many fields there are to replace
            //
            wordApp.Visible = true;
            oDoc = wordApp.Documents.Add( // load the template into a document workspace
                                               ref filename,  // and reference it through our myWordDoc
                                               ref missing,
                                               ref missing,
                                               ref missing);
            dynamic properties = oDoc.BuiltInDocumentProperties;
            // count how many fields we have to update
            int fields = oDoc.Fields.Count;

            foreach (Field myField in oDoc.Fields)
            {
                myField.Select();
                myField.Update();
            }
            oDoc.Save();
            oDoc.Close(ref notTrue, ref missing, ref missing);
            wordApp.Application.Quit(   ref notTrue,
                                        ref missing,
                                        ref missing);
        }
        catch (Exception excException)
        {
            MessageBox.Show(excOpenFile.Message +  System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace);
        }
    }

    public static void UpdateDocumentFieldsInFile()
    {
        string strFile = @"L:\admin\11ZG-0401\11-SWDev\Testing Field Updates (from Document Properties).docx";
        OpenDocument(strFile);
        Update(strFile);
    }
}

Where the main function calls UpdateDocumentFieldsInFile(). When I step through the code, it opens up the file and updates it, but after the program exits and I re-open the file manually, the fields have not been updated. Does anyone have any suggestions on how to resolve this? TIA.

  • 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-05-27T20:56:29+00:00Added an answer on May 27, 2026 at 8:56 pm

    Thanks again for the feedback, Dennis. After I made sure that I wasn’t opening the document again it still wouldn’t work for me for some reason that I haven’t been able to determine, so I eventually just ended up creating a program that uses Java Robot to do what I needed:

    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    
    public class Robot06{
      static int keyInput[] = 
      {
        KeyEvent.VK_F11,
        KeyEvent.VK_F9
      };
    
      public static void main(String[] args) throws AWTException,IOException
      {
        Runtime.getRuntime().exec("winword L:\\admin\\11ZG-0401\\11-SWDev\\\"Testing Field Updates (from Document Properties).docx\"");
        Robot robot = new Robot();
    
        for (int cnt2 = 0; cnt2 < 10; cnt2++)
        {
          robot.keyPress(keyInput[0]);
          robot.delay(500);
          robot.keyPress(keyInput[1]);
          robot.delay(500);
        }      
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to write some jQuery code that will expand a paragraph when
Present Scenario : I have a set of classes that all take a common
I am attempting to retrieve information on all the various monitors (and their adapters)
Presently I am attempting to create unit tests using MSTest.exe for multithreading functionality. When
I am attempting to connect to an Access 2000 database file (*.mdb), but I
Presented alongside a polling facility on a webpage is the following input fields that
I am attempting to write a C++ wrapper of OpenVG, which is very Open-GL
I'm presently attempting to write a few classes to aid me in writing various
Here is where I am at presently. I am designing a card game with
I'm attempting to find a solution to allow users to sort content that is

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.