1

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

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

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

4

1 回答 1

2

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

// 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());
    }
}
于 2015-05-05T05:26:00.427 回答