C
Clive Tolley
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
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