c# - 在单词互操作中使用选择和范围

标签 c# .net interop ms-word office-interop

范围和选择如何工作?

如果您通过一个范围在页面顶部插入内容,是否会被选择插入内容覆盖?选择会影响范围吗?

我在按此顺序插入时遇到问题,但 InlineShapes 显示在 Range.Inserts 上方的文档顶部。此外,Selection.InsertBefore 被 InlineShapes 取代。

我希望我的范围插入出现在每张图片上方,就像标题一样。如何做到这一点?

Selection.InsertBefore
**Loop**
     Range.InsertParagraphAfter
     Selection.InlineShapes.AddPicture
**End Loop** 

我已经问了三次这样的问题,但没有任何回应。任何事情都会有所帮助!

编辑:

我更近了一步。现在,当我添加 InlineShapes 时,我使用 Content.InlineShapes.AddPicture(FileName: x, range: y)。现在图片显示在段落之间,但它们位于我在添加图片之前添加到文档的表格的第一个单元格中。我添加了带有折叠范围的表格。

最佳答案

你写:

I've asked a question like this three times now with no responses. Anything would help!

作为引用,提到的问题(我猜)是:

...现在都已经回答了:)

然而,您在这篇文章中提出了多个问题 - 首先:

How do ranges and selections work?

实际上,文档很好地涵盖了这一点:

范围:

"Represents a contiguous area in a document.

Each Range object is defined by a starting and ending character position. Similar to the way bookmarks are used in a document, Range objects are used to identify specific portions of a document..."

引用:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range(v=office.14).aspx .

If you insert something at the top of the page through a range, is that overridden by inserting something with selection?

可以。这取决于所选择的内容。如果您选择的内容与您刚刚插入的内容的范围相交并更改内容,那么您将对该范围的内容进行更改。

要完全理解这一点,我建议您阅读:

Does selection affect ranges?

这取决于。文档指出:

Range objects are independent of the selection.

引用:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range(v=office.14).aspx

...所以如果您只是为了阅读内容或将内容复制到 剪贴板,什么都不会改变。但是,如果您使用选择来更改内容,这当然会影响范围指向的内容。接下来,您应该注意 MS 建议使用 Range 对象进行文档操作:

Because Range objects share many of the same methods and properties as Selection objects, using Range objects is preferable for manipulating a document when there isn't a reason to physically change the current selection.

引用:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.aspx

最后,你问:

I'm having a problem where I do inserts in this order, but the InlineShapes show up at the top of the document above the Range.Inserts. Also, the Selection.InsertBefore gets replaced by the InlineShapes.

I would like my range insert to appear above each picture, like a heading. How can this be done?

请参阅我之前对您问题的回答:“How to add items one at a time to to a new line a word document using word interop”,此处:https://stackoverflow.com/a/12751805/700926 - 给你一个完整的答案。

关于c# - 在单词互操作中使用选择和范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731665/

相关文章:

c# - 如何防止 C# 中因无效输入而崩溃

java - 从 C++ 头文件生成 Java 接口(interface)

c# - Excel 互操作工作表副本引发异常 HRESULT : 0x800A03EC

c++ - 一些基本的 COM 问题

c# - 释放临时 COM 对象

c# Entity Framework 选择派生子项

c# - SQLException 未提供的参数化查询 ###

c# - 使用 CLR 存储过程可以解决哪些好问题?

c# - Entity Framework Core 排除结果

c# - C#中的外部IP地址