Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

StoryRanges not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clive Tolley - 24 Jan 2005 01:03 GMT
Could someone please tell me why the following code works (i.e. goes through
the various Stories) ...

For Each aStory In ActiveDocument.StoryRanges
       Selection.HomeKey Unit:=wdStory, Extend:=wdMove
       
       With aStory.Find
           .Text = "text"
           .Replacement.Text = "XXXXXXXX"
           .Execute Replace:=wdReplaceAll
       End With

Next aStory

... but the following does not (and it makes no difference whether Range or
Selection is used, or what character the Range/selection is moved to):

   Dim aStory As Word.Range
   Dim aRange As Word.Range
   Dim strA As String
   Dim i As Integer
   
   Selection.HomeKey Unit:=wdStory, Extend:=wdMove
   Set aRange = ActiveDocument.Range(Start:=0, End:=0)
   
   For Each aStory In ActiveDocument.StoryRanges
       For i = 1 To aStory.Paragraphs.Count
           With aRange
               .MoveUntil Cset:=Chr$(13)
               .MoveStart Unit:=wdCharacter, Count:=-1
               .Select
           End With
           strA = aRange.Text
           Do While Asc(strA) = 32
               aRange.Collapse Direction:=wdCollapseStart
               aRange.Delete Unit:=wdCharacter, Count:=1
               aRange.MoveStart Unit:=wdCharacter, Count:=-1
               strA = aRange.Text
           Loop
           With aRange
               .MoveUntil Cset:=Chr$(13)
               .MoveStart Unit:=wdCharacter, Count:=1
           End With
       Next i
   Next aStory

Thanks
Jezebel - 24 Jan 2005 01:13 GMT
Strictly, your first snippet won't necessarily go through your entire
document. Some storyranges are necessarily a single range (like the
MainStory); others may be more than one. In particular headers and footers
if you have multiple sections and you clear the LinkToPrevious, and
textboxes, are in effect linked lists. To iterate all of them, you need
structure your loop along these lines --

For each aStory in ActiveDocument.StoryRanges

   Do
       .... process aStory
       Set aStory = aStory.NextStoryRange
   Loop until aStory is nothing

Next

That doesn't really answer your question; but I can't figure out what your
second lot of code is trying to do anyway. BTW, in your first snippet the
Selection statement is irrelevant. If you're working with Range objects
(which you should) then you don't need to select them to do your processing.

> Could someone please tell me why the following code works (i.e. goes through
> the various Stories) ...
[quoted text clipped - 43 lines]
>
> Thanks
Clive Tolley - 24 Jan 2005 01:37 GMT
Thanks, I'll try what you suggest there. I know the Selection doesn't need to
be there - it is left over from previous version, and as you see I'm trying
to use Range, but haven't refined it all yet. The second piece is supposed to
remove spaces before ends of paragraphs, including (in particular) ends of
footnotes, where the end of footnote 'paragraph' mark seems to be impervious
to coding (hence the aim of selecting (or ranging)  the previous character
and deleting it if it is a space. I dare say it's not elegant etc. - I'm not
that experienced. But in particular I want to ensure that moving through all
Stories is going to work, and this code won't move outside the main story at
all, which is baffling me.
Clive Tolley - 24 Jan 2005 02:27 GMT
Ah, now I see what I have done - very silly! I forgot to redefine the Range
as within the Story instead of the ActiveDocument. Now it works, with the
proper loop structure recommended previously.

> Thanks, I'll try what you suggest there. I know the Selection doesn't need to
> be there - it is left over from previous version, and as you see I'm trying
[quoted text clipped - 6 lines]
> Stories is going to work, and this code won't move outside the main story at
> all, which is baffling me.
Jezebel - 24 Jan 2005 02:50 GMT
Glad it's working. Are you aware you can do this with Find and Replace?
Search for: ^w^p and replace with ^p. Make sure the search direction is set
to ALL (otherwise Find looks only in the body of the document).

> Ah, now I see what I have done - very silly! I forgot to redefine the Range
> as within the Story instead of the ActiveDocument. Now it works, with the
[quoted text clipped - 10 lines]
> > Stories is going to work, and this code won't move outside the main story at
> > all, which is baffling me.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.