VSTO C# Shape.TextFrame.HasText 或 shape.Type 抛出 ComException(升级后)

问题描述 投票:0回答:0

一些一直没有问题的代码刚刚开始抛出 ComExceptions。给你一些背景..

Document wordTemplate = {a word document}
foreach (Shape shape in wordTemplate.Shapes)
{
    if (shape?.TextFrame != null && shape.TextFrame.HasText != 0)
    {
        foreach (Field fld in shape.TextFrame.TextRange.Fields)
        {
            if (fld.Type == WdFieldType.wdFieldMergeField)
            {
                fld.Select();
                FindReplaceMergeField(wordApp, fld.Code.Text);
                fld.Update();
                
            }
        }
        shape.TextFrame.TextRange.Fields.Update();
    }
}

void FindReplaceMergeField(_Application wordApp, string fieldText)
{
    wordApp.Selection.Text = fieldText;   
}

换句话说,打开一个 word 文档,遍历以找到“mergefields”。如果文档中有 TextBox,则在迭代形状时抛出 comException。

有人看过吗?

注意:在最近的安全更新之前,在 office/word 2019 中工作正常。

谢谢大家的宝贵时间。

c# vsto
© www.soinside.com 2019 - 2024. All rights reserved.