Do until Selection.Endkey Loop

P

Petra Liverani

I have recorded two macros and put
"Do Until Selection.Endkey" before and "Loop" after them.
One works and the other doesn't and I have no idea why.

'This one works - it involves finding some text, deleting
it, then copying adjacent text, finding upwards other text
and pasting the text previously found

Do Until Selection.EndKey

Selection.Find.ClearFormatting
With Selection.Find
.Text = "~~~"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
Selection.Copy
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Answer="
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
Loop

'This one doesn't work - it involves finding a style,
moving to the next paragaph and selecting it and making
that paragraph a particular style

Do Until Selection.EndKey

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading
2")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("LD Body
Indent")

Loop
 
D

Doug Robbins - Word MVP

Hi Petra,

The way to do this is to use

Do While.Execute(FindText: = etc) = True
'actions
Loop

as shown in the following example:

' Macro to round all numbers in a document
' Macro created 19/7/00 by Doug Robbins
'
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9]{3,}", MatchWildcards: =
True, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Range.Text = Round(Selection.Range.Text, 2)
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Petra Liverani

Thanks very much for your reply Doug. I have modified your
code as best I can but it still doesn't work - however,
I'm not sure about code for searching for Style as opposed
to Text. I wonder if this difference is a factor as the
macro I wrote that does work looks for text instead of
style. Below is my adapted code. I get an error message on
the Do While line saying object missing.

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindStyle:="Heading 2",
Wrap:=wdFindContinue, Forward:=True) = True
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("LD Body Indent")
Loop



-----Original Message-----
Hi Petra,

The way to do this is to use

Do While.Execute(FindText: = etc) = True
'actions
Loop

as shown in the following example:

' Macro to round all numbers in a document
' Macro created 19/7/00 by Doug Robbins
'
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9] {3,}", MatchWildcards: =
True, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Range.Text = Round (Selection.Range.Text, 2)
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Petra Liverani said:
I have recorded two macros and put
"Do Until Selection.Endkey" before and "Loop" after them.
One works and the other doesn't and I have no idea why.

'This one works - it involves finding some text, deleting
it, then copying adjacent text, finding upwards other text
and pasting the text previously found

Do Until Selection.EndKey

Selection.Find.ClearFormatting
With Selection.Find
.Text = "~~~"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
Selection.Copy
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Answer="
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
Loop

'This one doesn't work - it involves finding a style,
moving to the next paragaph and selecting it and making
that paragraph a particular style

Do Until Selection.EndKey

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles ("Heading
2")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("LD Body
Indent")

Loop


.
 
D

Doug Robbins - Word MVP

Hi Petra,

Use the following:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
Do While .Execute(FindText:="", Wrap:=wdFindStop, Forward:=True) = True
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.Style = ActiveDocument.Styles("LD Body Indent")
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Petra Liverani said:
Thanks very much for your reply Doug. I have modified your
code as best I can but it still doesn't work - however,
I'm not sure about code for searching for Style as opposed
to Text. I wonder if this difference is a factor as the
macro I wrote that does work looks for text instead of
style. Below is my adapted code. I get an error message on
the Do While line saying object missing.

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindStyle:="Heading 2",
Wrap:=wdFindContinue, Forward:=True) = True
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("LD Body Indent")
Loop



-----Original Message-----
Hi Petra,

The way to do this is to use

Do While.Execute(FindText: = etc) = True
'actions
Loop

as shown in the following example:

' Macro to round all numbers in a document
' Macro created 19/7/00 by Doug Robbins
'
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9] {3,}", MatchWildcards: =
True, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Range.Text = Round (Selection.Range.Text, 2)
Loop
End With

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Petra Liverani said:
I have recorded two macros and put
"Do Until Selection.Endkey" before and "Loop" after them.
One works and the other doesn't and I have no idea why.

'This one works - it involves finding some text, deleting
it, then copying adjacent text, finding upwards other text
and pasting the text previously found

Do Until Selection.EndKey

Selection.Find.ClearFormatting
With Selection.Find
.Text = "~~~"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
Selection.Copy
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Answer="
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
Loop

'This one doesn't work - it involves finding a style,
moving to the next paragaph and selecting it and making
that paragraph a particular style

Do Until Selection.EndKey

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles ("Heading
2")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("LD Body
Indent")

Loop


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top