Using selection and ranges in word interop

15,881

You write:

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

And for reference, the questions referred to (I guess) are:

... which all now have been answered :)

You ask multiple questions in this post however - first:

How do ranges and selections work?

Actually, the documentation covers this quite well:

A range:

"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..."

Reference: 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?

It can be. This depends on what has been selected. If you select something that intersects with the range of the content you just inserted and changes the content, then you will make changes to the content of the range.

To understand this completely, I will recommend that you read about:

Does selection affect ranges?

It depends. The documentation states:

Range objects are independent of the selection.

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

... so if you just do a simple selection in order to, e.g., read content or copy content to the clipboard, nothing would change. However, if you use the selection to change content, of course this would affect the content pointed to by the ranges. In continuation of this you should note that MS recommends using Range objects for document manipulation:

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.

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

And finally, you ask:

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?

See my previous answer to your question: " How to add items one at a time to to a new line a word document using word interop " here: https://stackoverflow.com/a/12751805/700926 - that gives you a complete answer.

Share:
15,881
sixshift04
Author by

sixshift04

Updated on June 04, 2022

Comments

  • sixshift04
    sixshift04 almost 2 years

    How do ranges and selections work?

    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?

    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?

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

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

    EDIT:

    I'm a step closer. Now, when I add my InlineShapes, I use Content.InlineShapes.AddPicture(FileName: x, range: y). Now the pictures are showing up in between paragraphs, but they are in the first cell of a table I add to the document before adding the picture. I add the table with with a collapsed range.