BUG:Word 2013 VSTO无法处理文本后面或前面格式的页眉中的图像

Sam*_*ngh 7 c# vsto ms-word com-interop word-2013

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

我注意到使用VSTO处理文档时特定于Word 2013的问题.

该文档在页眉或页脚中包含一个图像,其布局选项设置为"With Text Wrapping",带有"Behind Text"或"In Text of Text":

图像的布局选项设置为

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

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Run Code Online (Sandbox Code Playgroud)

我在这里上传了一个repro: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;
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

抛出异常,无论我是否尝试捕获它,它都会崩溃Word 2013:

事件查看器条目

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

有什么办法可以让我在Word 2013上工作吗?我尝试了许多假想的修复,包括多次修复和重新安装Office 2013,但无济于事.

Fre*_*rik 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);
}
Run Code Online (Sandbox Code Playgroud)

不知道这对你有帮助吗?(我不是 Word Interop 专家..)