BUG: Word 2013 VSTO cannot process an image in a header formatted behind or in front of text

I cross-post this question from the Microsoft community because I did not get any answer there, and maybe someone here can shed some light on this.

I noticed a problem that applies to Word 2013 when using VSTO to process a document.

The document contains an image in the header or footer, in which its layout settings are set to “With text wrap” using “Behind text” or “Before text”:

Image with layout options set to

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

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

I uploaded the reproduction here: Word2013VstoImageFormattedInHeaderBug.zip

The corresponding code snippet 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 complete 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 

An exception is thrown regardless of whether I try to catch it or not, and it resets Word 2013:

Event View Record

This error does not occur in Word 2016, and I can successfully process forms. However, upgrading to Office 2016 is not an option. I believe that fixing a bug requires a fix for Office 2013.

Is there anything I can do to make this work in Word 2013? I tried many alleged fixes, including several repairs and reinstallations of Office 2013, but to no avail.

+7
c # ms-word word-2013 vsto com-interop
source share
2 answers

Forms can be placed anywhere on the page, but they are always tied to a range of text anchors. Choosing the current range before iterating over a figure tied to it can enable the exception HRESULT 0x80010105 RPC_E_SERVERFAULT .

In your GetAllFields() method, select () the range of header and footer 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 do not know if this will help you? (I'm not an expert at Word Interop ..)

+1
source share

I work a lot on this comment in the WordFieldEnumerator class

  //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 works as soon as possible without errors; an error on the figures in the header and footer they have a problem with access

0
source share

All Articles