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 ;.)
You can! You have two options:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15
(or wherever the office/sharepoint workload would have installed)Microsoft.Office.Word.Interop.dll
) and the office.dll
file into a folder in your project (e.g. yourProject\lib
).dll
s in your .csproj file:<ItemGroup>
<Reference Include="lib\Microsoft.Office.Interop.Word.dll" />
<Reference Include="lib\Office.dll" />
</ItemGroup>
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.
Hope this helps!