PDA

View Full Version : Solved: Range.Offset



jasoncw
04-09-2008, 07:28 AM
I am trying to determine how to define a range variable using an object, but I want the range to be just before the object. How would I go about this?

For example, Excel has an offset property to define this [Range("A1").Offset(1, 1)]. How is this accomplished in Word? This is how I can define the range as the object itself (which is an embedded worksheet object):
Set wdRng = ActiveDocument.InlineShapes(1).Range
How do I change this, so that the range is the line above the object?

TIA

Jason

fumei
04-09-2008, 09:36 AM
"the line above the object?"

Do you mean paragraph?

There is no Offset in Word.

This seems very odd to me. Setting a range object to a specific object's range....then making it NOT that object's range.

Ranges are numbers. If the point is to make a range object with Start/End numbers that are just before the InlineShape....then, why not use those numbers? Like so:

Dim r As Range
Dim myStart As Long

myStart = ActiveDocument.InlineShapes(1).Range.Start
Set r = ActiveDocument.Range(Start:=myStart - 1, _
End:=myStart - 1)
r.Expand Unit:=wdParagraph


This sets the range object ( r ) with explicit numbers, i.e the Start of the InlineShape(1).Range

In the above example, I expanded the range object to the paragraph.

In other words, it makes a range object of the paragraph just before InlineShape(1).

Note the only reason I used a variable (myStart) is to make the code lines simple to read. Technically it could be:
Dim r As Range
Set r = ActiveDocument.Range(Start:=ActiveDocument _
.InlineShapes(1).Range.Start - 1, _
End:=ActiveDocument.InlineShapes(1) _
.Range.Start - 1)
r.Expand unit:=wdParagraph


They are exactly the same.

jasoncw
04-09-2008, 09:52 AM
Perfect. That's exactly what I needed. Thanks, Gerry. I'm still trying to get a handle on Word ranges... And yes, I meant the paragraph above.

Jason

fumei
04-09-2008, 10:02 AM
"And yes, I meant the paragraph above."

I thought you may have. Always a good idea to be very explicit though...as many here have read my rants on this.

"I'm still trying to get a handle on Word ranges"

Good for you. Yeah, it takes a while, but when you DO, it will make a big, big, difference to how you use VBA in Word.

Range is one of the most important concepts in Word. Personally, I think it is #1, but Paragraph gives it a serious run.