0

I am currently creating a macro that needs to save Word.Range instances to an array. I want those range objects to be anywhere in the document. For the regular content stuff this works just fine, but when I am trying to save a range in the header, I cannot get it to work.

The sub definition:

Function GetSelectionRanges(rRng As Object, sFind As String) As Variant

rRng is a StoryRange

The code for the regular conntent ranges is as follows:

Set tmpSelections(i) = WdDoc.Range(Start:=rRng.Start, End:=rRng.End)

This one works absolutely fine and I can work with it. I tried to extend it so it can store header objects as well:

Set tmpSelections(i) = WdDoc.StoryRanges(rRng.storytype).Range(Start:=rRng.Start, End:=rRng.End)

This, however, does not work. It says object does not support this attribute/method.

My problem at the moment is that the start and end are obtained correctly but if I use the WdDoc.Range method it uses the text stored in the regular content and thus not working as I want it to.

Definitions:

WdDoc As Object
tmpSelections() As Object

Later on I want to work with the single ranges and eventually replace values but that is a later step that cannot be incorporated in that routine.

1 Answer 1

1

The StoryRanges property returns a collection of Range objects, so you don't need to call .Range.

If you want to select a range within the StoryRange, use SetRange:

Set tmpSelections(i) = wdDoc.StoryRanges(rRng.StoryType)
tmpSelections(i).SetRange Start:=rRng.Start, End:=rRng.End
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is I want only a certain number of characters not an entire range saved to the array.
The code above does the same as in your original example, ie it takes the whole range. If you'd like to take only part of the range, you can adjust the range eg using sr.SetRange 0, 100.
Set tmpSelections(i) = WdDoc.StoryRanges(rRng.storytype).SetRange(Start:=rRng.Start, End:=rRng.End) I get an error that an object is required, but I feel that the SetRange method is used incorrectly.
SetRange modifies the existing range rather than returning a new one, so you don't use it as an assignment ie set x = range.SetRange(start,end). You use it as a statement ie range.SetRange start,end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.