错误!未知的条件操作码

Error! Unknown op code for conditional

我正在使用 Aspose.Words 进行 MailMerge。但是在合并字段后,它显示 错误!文档本身中条件 的未知操作码。此错误可能是由于合并字段格式不正确造成的。但是我的要求是通过代码detect/catch这样的错误。因为,在我们的案例中,用户自己创建了单词模板并上传到系统中。我写了非常简单的代码来阅读邮件合并。

doc.MailMerge.Execute(this.DataSource.Rows[rowIndex];

我们能在代码中检测到这样的错误吗?我试图在网上找到,但没有找到任何有用的东西。

这种情况下不会抛出异常,但是可以使用合并后字段的结果进行catch。试试下面的示例代码

// Load the document
Aspose.Words.Document doc = new Aspose.Words.Document(src);
// Do processing and mail merge etc

// Select all field start nodes so we can find the merge fields.
NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart fieldStart in fieldStarts)
{
    // Get the next sibling
    Run fieldResult = (Run)fieldStart.NextSibling;

    // Match the error code with the result
    if (fieldResult.NextSibling.NextSibling.GetText().Equals("Error! Unknown op code for conditional.", StringComparison.CurrentCultureIgnoreCase))
    {
        // Find the page number, where the field is present
        LayoutCollector collector = new LayoutCollector(doc);
        int pageNumber = collector.GetStartPageIndex(fieldStart);
        Console.WriteLine("Error in field at Page: " + pageNumber + ". Field text: " + fieldResult.GetText());
    }
}