BUG:Word 2013 VSTO 无法处理格式为 Header 的图像在文本后面或前面
BUG: Word 2013 VSTO Cannot Handle Image in Header Formatted Behind or In Front of Text
I am cross-posting this question from Microsoft Community 因为我在那里没有得到任何回应,也许这里有人可以对此有所启发。
我注意到使用 VSTO 处理文档时 Word 2013 特有的问题。
该文档在 header 或页脚中包含一个图像,其布局选项设置为 "With Text Wrapping",并且 "Behind Text" 或 "In Front of Text":
如果我使用 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 崩溃:
这个bug在Word 2016上没有出现,我可以成功处理形状。但是,升级到 Office 2016 不是一种选择。我认为这需要 Office 2013 的修补程序才能修复错误。
我可以做些什么来让它在 Word 2013 上运行吗?我尝试了很多假定的修复,包括多次修复和 re-installs Office 2013,但都无济于事。
形状可以放置在页面上的任何位置,但它们始终附加到文本的锚定范围内。 Select 在迭代锚定到它的形状之前调整当前范围可能会解决 HRESULT 0x80010105 RPC_E_SERVERFAULT
异常。
在您的 GetAllFields()
方法中, Select() 在调用 ProcessShapes()
.[=14 之前分别 页眉和页脚的范围 =]
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 互操作专家..)
在 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);
// }
//}
它工作得尽可能好,没有错误;
错误是形状在页眉和页脚中,它们有访问问题
I am cross-posting this question from Microsoft Community 因为我在那里没有得到任何回应,也许这里有人可以对此有所启发。
我注意到使用 VSTO 处理文档时 Word 2013 特有的问题。
该文档在 header 或页脚中包含一个图像,其布局选项设置为 "With Text Wrapping",并且 "Behind Text" 或 "In Front of Text":
如果我使用 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 崩溃:
这个bug在Word 2016上没有出现,我可以成功处理形状。但是,升级到 Office 2016 不是一种选择。我认为这需要 Office 2013 的修补程序才能修复错误。
我可以做些什么来让它在 Word 2013 上运行吗?我尝试了很多假定的修复,包括多次修复和 re-installs Office 2013,但都无济于事。
形状可以放置在页面上的任何位置,但它们始终附加到文本的锚定范围内。 Select 在迭代锚定到它的形状之前调整当前范围可能会解决 HRESULT 0x80010105 RPC_E_SERVERFAULT
异常。
在您的 GetAllFields()
方法中, Select() 在调用 ProcessShapes()
.[=14 之前分别 页眉和页脚的范围 =]
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 互操作专家..)
在 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);
// }
//}
它工作得尽可能好,没有错误; 错误是形状在页眉和页脚中,它们有访问问题