7

我从 Microsoft 社区交叉发布了这个问题,因为我在那里没有得到任何回复,也许这里有人可以对此有所了解。

在使用 VSTO 处理文档时,我注意到 Word 2013 特有的问题。

该文档在页眉或页脚中包含一个图像,其布局选项设置为“带文本环绕”,“文本后”或“文本前”:

布局选项设置为的图像

使用 VSTO,如果我打开文档然后尝试处理形状,则会收到以下异常:

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

我在这里上传了一个重现:Word2013VstoImageFormattedInHeaderBug.zip

相关的代码在WordFieldEnumerator.cs

private static bool ShapesWithinGroup(Shape shape)
{
    var result = false;
    try
    {
        // shape.GroupItems throws the exception
        if (shape.GroupItems != null && shape.GroupItems.Count > 0)
        {
            result = true;
        }
    }
    catch (UnauthorizedAccessException)
    {
        // This shape is not in a group - ignore
    }
    catch (Exception exception)
    {
        var exceptionString = exception.BuildExceptionString();
        Console.WriteLine(exceptionString);
        Console.WriteLine(exception.StackTrace);
        //throw;
    }

    return result;
}

这是完整的异常和堆栈跟踪:

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

   at Microsoft.Office.Interop.Word.Shape.get_GroupItems()
   at Word2013VstoImageFormattedInHeaderBug.WordFieldEnumerator.ShapesWithinGroup(Shape shape) in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\WordFieldEnumerator.cs:line 170
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

   at Microsoft.Office.Interop.Word.Shape.get_TextFrame()
   at Word2013VstoImageFormattedInHeaderBug.WordFieldEnumerator.ProcessShapes(IEnumerable`1 shapes) in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\WordFieldEnumerator.cs:line 124
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.CustomMarshalers.EnumeratorViewOfEnumVariant.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
   at Word2013VstoImageFormattedInHeaderBug.WordFieldEnumerator.ProcessShapes(IEnumerable`1 shapes) in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\WordFieldEnumerator.cs:line 90
   at Word2013VstoImageFormattedInHeaderBug.WordFieldEnumerator.GetAllFields() in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\WordFieldEnumerator.cs:line 64
   at Word2013VstoImageFormattedInHeaderBug.Program.LockDialogFields(Document document) in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\Program.cs:line 116
   at Word2013VstoImageFormattedInHeaderBug.Program.PdfDocument(String documentFilePath) in C:\Users\QA\Desktop\Word2013VstoImageFormattedInHeaderBug\Word2013VstoImageFormattedInHeaderBug\Program.cs:line 60

无论我是否尝试捕获它,都会引发异常,并且它会使 Word 2013 崩溃:

事件查看器条目

Word 2016 上不会出现此错误,我可以成功处理形状。但是,不能升级到 Office 2016。我认为这需要 Office 2013 的修补程序才能修复该错误。

我能做些什么来让它在 Word 2013 上运行吗?我尝试了许多假定的修复程序,包括多次修复和重新安装 Office 2013,但均无济于事。

4

2 回答 2

1

形状可以放置在页面上的任何位置,但它们始终附加到文本的锚定范围。在迭代锚定到它的形状之前选择当前范围可能会解决HRESULT 0x80010105 RPC_E_SERVERFAULT异常。

在您的GetAllFields()方法中, Select()在调用之前分别选择页眉和页脚的范围ProcessShapes()

foreach (HeaderFooter header in section.Headers)
{
    if (header.LinkToPrevious) // || header.Index != WdHeaderFooterIndex.wdHeaderFooterFirstPage
    {
        continue;
    }

    header.Range.Select();

    // Add the fields in the header
    fields.AddRange(header.Range.Fields.Cast<Field>());

    // Add the fields in the shapes in the header
    var fieldsInShapes = ProcessShapes(header.Shapes.Cast<Shape>());
    fields.AddRange(fieldsInShapes);
}

foreach (HeaderFooter footer in section.Footers)
{
    if (footer.LinkToPrevious) // || footer.Index != WdHeaderFooterIndex.wdHeaderFooterFirstPage
    {
        continue;
    }

    footer.Range.Select();

    // Add the fields in the footer
    fields.AddRange(footer.Range.Fields.Cast<Field>());

    // Add the fields in the shapes in the footer
    var fieldsInShapes = ProcessShapes(footer.Shapes.Cast<Shape>());
    fields.AddRange(fieldsInShapes);
}

不知道这对你有没有帮助?(我不是 Word 互操作专家..)

于 2017-05-05T09:39:39.370 回答
0

在WordFieldEnumerator类中评论此代码后,我做了很多工作

  //foreach (Section section in Document.Sections)
            //{
            //    foreach (HeaderFooter header in section.Headers)
            //    {
            //        if (header.LinkToPrevious) // || header.Index != WdHeaderFooterIndex.wdHeaderFooterFirstPage
            //        {
            //            continue;
            //        }
            //        // Add the fields in the header
            //        fields.AddRange(header.Range.Fields.Cast<Field>());

            //        // Add the fields in the shapes in the header
            //        var fieldsInShapes = ProcessShapes(header.Shapes.Cast<Shape>());
            //        fields.AddRange(fieldsInShapes);
            //    }

            //    foreach (HeaderFooter footer in section.Footers)
            //    {
            //        if (footer.LinkToPrevious) // || footer.Index != WdHeaderFooterIndex.wdHeaderFooterFirstPage
            //        {
            //            continue;
            //        }
            //        // Add the fields in the footer
            //        fields.AddRange(footer.Range.Fields.Cast<Field>());

            //        // Add the fields in the shapes in the footer
            //        var fieldsInShapes = ProcessShapes(footer.Shapes.Cast<Shape>());
            //        fields.AddRange(fieldsInShapes);
            //    }
            //}

它工作得尽可能好,没有错误;错误在于形状在页眉和页脚中 他们有访问问题

于 2017-05-08T20:24:19.223 回答