Loop Macro

G

Guest

Wood 2003

In an effort to begin the learning process of VBA I am attempting to create
some uncomplicated macros which hopefully apply to any document with which I
am actively involved. However, I am finding that thus far even the most
rudimentary and simplest of the VBA syntax continues to escape me.

In this particular instance, I would like a macro that searches for two
consecutive paragraph returns and tests the *style* of the second of the
consecutive paragraph returns. If the *style* of the second paragraph return
is something other than *normal*, I would like for the Macro to change the
*style* of this second paragraph return to *normal*.

I would like for the macro to continue in this effort until such time as it
reaches the end of the document.

The following is what I have come up with so far through the use of the
Macro Recorder. The ability to fill in the remaining necessary coding and to
then get the coding to repeat until the end of the document is what is
currently causing me problems.

Sub LoopThroughDocument()
'
selection.Find.ClearFormatting
With selection.Find
.Text = "^p^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
selection.Find.Execute
selection.MoveRight Unit:=wdCharacter, Count:=1
selection.MoveLeft Unit:=wdCharacter, Count:=1
If selection.Style <> "Normal" Then
selection.Style = "Normal"
End If
Loop

End Sub

If any of you is able to come up with suggestions, I would be very
appreciative.

Thanks in advance – Rod
 
H

Helmut Weber

Hi RPMitchal,

like that:

Sub Macro3aa()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "^p^p"
While .Execute
' rDcm.Select ' for testing
rDcm.Characters.Last.Style = "normal"
Wend
End With
End Sub

Whether the style (paragraph style!) is "normal" anyway,
doesn't matter much.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Guest

Helmut:

Thank you so much for the coding. As soon as I return to the office on
Monday, I'll give it a try. However, in reading through it, it seems rather
clear cut to me. Please wish me luck with future VBA endeavors.

It is quite a mindboggling undertaking, but I'm hoping to persevere. :)

With much appreciation... Rod
 
G

Guest

Hello Again Helmut.

I have just sent you a response in appreciation for the coding that you
provided in this regard. I've read through the coding that you furnished
(I've not yet had the opportunity to put it to the actual test).

However, once again and with my limited knowledge of VBA it would seem to me
that any coding that would cause the macro to loop through to the end of the
document appears to be missing or is it just me?

I was under the impression that there should be a *do* and and *loop*
execution involved within. Yes? No?

Once again, with appreciation, I remain...

Rod
 
J

Jay Freedman

I suspect Helmut went to bed hours ago, so I'll answer for him...

The While and Wend statements serve the same purpose as Do and Loop.
The code could equally well be written as

Do While .Execute
' rDcm.Select ' for testing
rDcm.Characters.Last.Style = "normal"
Loop

and it would behave exactly the same.
 
G

Guest

Hello Jay:

Thanks so very much for stepping in on this. I appreciate the assistance
received from both you and Helmut in this regard. As for my continued study
of VBA - it's upward and onward for me (frustrations included). I'm sure
that you will be hearing from me again as time goes on.

Very truly yours - Rod
 

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