Search code examples
c#office365office-interop

Can I use the WordInterop functionality with Office365?


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 ;.)


Solution

  • You can! You have two options:

    Option 1
    1. Install the office/sharepoint development workload in Visual Studio Installer
    2. Navigate to C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15 (or wherever the office/sharepoint workload would have installed)
    3. Copy the desired library (i.e. Microsoft.Office.Word.Interop.dll) and the office.dll file into a folder in your project (e.g. yourProject\lib)
    4. Reference these .dlls in your .csproj file:
    <ItemGroup>
        <Reference Include="lib\Microsoft.Office.Interop.Word.dll" />
        <Reference Include="lib\Office.dll" />
    </ItemGroup>
    
    Option 2

    Add the COM reference for Word to the .csproj file:

    <ItemGroup>
      <COMReference Include="Microsoft.Office.Interop.Word">
        <WrapperTool>tlbimp</WrapperTool>
        <VersionMinor>7</VersionMinor>
        <VersionMajor>8</VersionMajor>
        <Guid>00020905-0000-0000-c000-000000000046</Guid>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
        <EmbedInteropTypes>true</EmbedInteropTypes>
      </COMReference>
    </ItemGroup>
    

    It may be easier to do this through the Visual Studio UI by right clicking your project's dependencies and clicking add COM reference..., then selecting Microsoft Word 16.0 Object Library. The same applies for excel- just make sure that Microsoft Excel 16.0 Object Library is ticked.

    If this doesn't work then uninstall the Interop NuGet package and try again.

    Remarks
    • It's probably worth noting that ideally you would use a tool such as OpenXml to create the .docx instead of using the interop.
    • Using option 2 can lead to problems when using .NET Core - I had problems trying to build my project that contained a Word COM reference as COM references are not supported in the .NET Core version of msbuild. Using option 1 does not have this issue.

    Hope this helps!