BUG: Word 2013 VSTO Cannot Handle Image in Header Formatted Behind or In Front of Text

7

I am cross-posting this question from Microsoft Community because I haven't gotten any response there, and maybe someone here can shed some light on this.

I have noticed an issue that is specific to Word 2013 when using VSTO to process a document.

The document contains an image in the header or footer that has its Layout Options set to "With Text Wrapping" with either "Behind Text" or "In Front of Text":

Image with Layout Options set to "With Text Wrapping" with "Behind Text"

Using VSTO, if I open the document and then attempt to process shapes, I get the following exception:

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

I have uploaded a repro here: Word2013VstoImageFormattedInHeaderBug.zip

The relevant piece of code is in 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;
}

Here is the full exception and stacktrace:

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

The exception is thrown regardless of whether I attempt to catch it or not, and it crashes Word 2013:

Event Viewer entry

This bug does not occur on Word 2016, and I can process shapes successfully. However, upgrading to Office 2016 is not an option. I am of the opinion that this requires a hotfix for Office 2013 in order for the bug to be fixed.

Is there anything I can do to get this working on Word 2013? I have tried numerous supposed fixes, including multiple repairs and re-installs of Office 2013, to no avail.

c#
ms-word
vsto
com-interop
word-2013
asked on Stack Overflow May 1, 2017 by Sameer Singh

2 Answers

1

Shapes can be positioned anywhere on the page, but they are always attached to an anchoring range of text. Selecting the current range before iterating over the Shapes anchored to it may resolve the HRESULT 0x80010105 RPC_E_SERVERFAULT exception.

In your GetAllFields() method, Select() the range of the header and footer respectively before calling 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);
}

I don't know if this helps you? (I'm no Word Interop expert..)

answered on Stack Overflow May 5, 2017 by Fredrik
0

i work on it a lot after commented this code at class 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);
            //    }
            //}

it work as well as possible with no error; the error is on shapes are in header and footer they have access problem

answered on Stack Overflow May 8, 2017 by Shahrooz Ansari • edited May 8, 2017 by Shahrooz Ansari

User contributions licensed under CC BY-SA 3.0