Jay
Tue Jan 11 16:26:10 CST 2005
Hi Greg,
While you can use the Selection's properties to find out where the user left
it, don't move it or select something else unless there's no other way to
get what you need.
Play with this for a bit:
Dim nStory As WdStoryType
Dim oRg As Range
nStory = Selection.StoryType
Set oRg = ActiveDocument.StoryRanges(nStory)
MsgBox oRg.Words.Count & " words"
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:
http://word.mvps.org
Greg Maxey wrote:
> Jay,
>
> Thank for the explanation. I am goofing around with a little macro
> (just for practice) that counts the number words in a document(story)
> from the a list of words. If the users puts the cursor in a header
> the cursor needs to be moved to the start to get a accurate count.
> There is no way of knowing in advance where the user will select so
> setting the the a range in advance doesn't appear to be possible.
>
> I trid a construction similiar to Seletion.Tables(1).Range.Select but
> there doesn't seem to be a Selection.CurrentStory :-(
>
> Thanks for the continuing lessons.
>
>
> Jay Freedman wrote:
>> Greg wrote:
>>> Hi,
>>>
>>> ActiveDocument.Range(0,0).Select
>>>
>>> moves the IP to the start of the maintext story apparently from any
>>> story.
>>>
>>> My IP is in the header story and I want to move the IP to the start.
>>>
>>> I can use
>>>
>>> Selection.HomeKey Unit:=wdStory
>>>
>>> I was wondering if there isn't a Range(Start, End) method that I
>>> should be using.
>>>
>>> Thanks
>>
>> Hi Greg,
>>
>> No, HomeKey is entirely appropriate.
>>
>> BUT I strongly advise not using the Selection in headers/footers if
>> you can possibly avoid it. The movement of the cursor in headers,
>> especially with multiple sections and unknown layouts, can be almost
>> impossible to predict. Instead, set a Range object equal to the range
>> of the appropriate story, then if necessary collapse it to the start
>> or to some smaller unit, and (WITHOUT using Select) modify its .Text
>> or other properties.
>>
>> For example, if I wanted to add a page number field at the left side
>> of the main header of Section 1, I'd do this:
>>
>> Dim oRg As Range
>>
>> Set oRg = ActiveDocument.Sections(1) _
>> .Headers(wdHeaderFooterPrimary).Range
>>
>> oRg.Collapse wdCollapseStart
>>
>> oRg.Fields.Add Range:=oRg, Type:=wdFieldPage
>>
>> FWIW, I almost never use the Range(Start, End) construction either.
>> That's just my preference.