1

I have to refactorate very old Code (older than 2005). The Software should open Word Documents and fill variables in the header with data. The Doc will open, but the variables are empty. I use following codesnipped to show the amount of var´s in a Word Document to do some tests. It always say´s there are 0... Can I use the WordInterop with Office 365 ?

using Microsoft.Office.Interop.Word;

namespace CheckForVariablesInWord
{
    class Program
    {
        static void Main(string[] args)
        {           
            Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
            Document document = ap.Documents.Open(@"C:\temp\TestDocument.docx");
            ap.Visible = true;
            System.Console.WriteLine(document.Variables.Count);
            System.Console.ReadLine();
        }
    }
}

this is the TestWordDocument:

Here is the Variable named „myTest”

{ DOCVARIABLE myTest * MERGEFORMAT}

thx if anyone can help me out before i get mindsick ;.)

silentw
  • 4,835
  • 4
  • 25
  • 45
7r33dom
  • 11
  • 1
  • The Interop is different for each version of Micorosoft Office. So if you update the Office you need to download the version of the Interop that is compatible. – jdweng Sep 09 '19 at 11:30
  • Since you are using *.docx files I would highly recommend not using Interop and rather making use of the OpenXml library. See https://stackoverflow.com/questions/26711828/how-to-get-all-merge-fields-of-word-document-using-open-xml-sdk – Gary Sep 09 '19 at 11:33
  • I installed the newest Version via NuGet for the Interop. It still don´t works. I would use the OpenXml Lib but I have to manage a huge uncommended Codebase. I don´t know what happens if I change something ;-( nevertheless thx for your help – 7r33dom Sep 09 '19 at 12:34

0 Answers0