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

2

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.

Deaf answered 1/5, 2017 at 23:7 Comment(4)
1. Have you updated your video driver? 2. Does turning off Anti-Virus/Firewall make any difference? 3. Have you tried renaming wwlib.dll and doing a office repair? 4. You are not running this on a server OS? 5. The code to work with Shapes is correct?Earful
@JeremyThompson 1. Yes. 2. No. 3. Yes. 4. No, Windows 10 64-bit version 1511 (OS Build 10586.318), Word 2013 32-bit 15.0.4823.1000 (MSO 15.0.4823.1000), part of Office Standard 2013. 5. Yes. The bug still occurs, and Word still crashes. Is there anything else I should check?Deaf
Does it still occur if you check the Type of the shape instead of catching an exception when it's not a group?Dupion
@Dupion believe it or not, yes. Word 2013 crashes just the same.Deaf
L
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..)

Lithophyte answered 5/5, 2017 at 9:39 Comment(3)
That's interesting and something not very obvious if one focuses on shapes instead of ranges. I will give this a shot and let you know how it goes. Thanks for responding!Deaf
I am very excited about this, our QA department has tentatively approved this fix for Word 2013! They just have a few more tests to run before officially marking the bug as fixed. I will then award the bounty to you, well-deserved! I cannot express how utterly dumbfounded I am (not to mention dumb), but this is VSTO and COM so I guess solutions to these sort of problems are expected to come out of left field! Thanks again!Deaf
Hi Sameer. I think many feel the Office Interop/VSTO SDKs are tricky to work with to say the least (myself included). I am glad things are on the right track again for you and the team and thanks for the bounty. Happy coding! / FredrikLithophyte
A
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

Acuate answered 8/5, 2017 at 20:24 Comment(1)
Unfortunately no, commenting out the code that causes Word 2013 to crash does not constitute a fix. Sorry.Deaf

© 2022 - 2024 — McMap. All rights reserved.