Search code examples
.netnetoffice

Perform Word mail merge programmatically using NetOffice


I'm trying out NetOffice to perform MS Word mail merge using a template and programatically filling in the merge fields. The examples available here does not really have this scenario. I updated Example 03 in it so that it creates a new document from an existing MS Word document with merge fields. I then changed it to access the merge fields using the code below:

var fields = newDocument.MailMerge.Fields;
foreach (Word.MailMergeField field in fields)
{
   ...
}

But the for loop gets this exception the first time it tries to enumerate through fields: Factory is not initialized with NetOffice assemblies. If I try to run on debug, fields.Count returns a correct count of the merge fields on my document. I just can't seem to access the fields to fill/update them.

Has anyone encountered this before?

UPDATE

Running a debug on Core.cs, it fails to load the assembly from WordApi.dll, and consequently does not get the FactoryInfo from that assembly, which I assume it needs in its _factoryList. I haven't quite figured out why it is failing to load the assembly even when the dll is in the folder that it is searching in.


Solution

  • The problem I'm getting appears to be a bug on NetOffice. The bug is in the CurrentAppDomain class' LoadFrom(). Given it is accepting the assembly's full file path (and not the assembly's fully qualified name), it should perform an Assembly.LoadFrom(path) if LoadAssembliesUnsafe is false.

    I have reported this bug/issue on github. Link here.