Code written for "Sections" doing "Paragraphs" instead

S

Sophia

Hello,

I have an rtf document open on Word97.

ActiveDocument.Sections.count =18

The sections are delimited by Section Breaks (Continuous)

I have, in the document, a list of words stored as Document
Variables.

I'm trying to find the first instance in each "Section"
of a word from that list and set the font for that "found" word
to "bold"... then go on through the list of words, setting any
other of the words from the list to bold ... then go on to the
next Section.

For some reason, instead of doing "For each Section", the
following code is doing "For each Paragraph" instead.

What am I doing wrong?

Public Sub aSetDocVarsToBold()

Dim mySCTN As Section
Dim oControl As Control
Dim i, SectionNumber
Dim myvar As Variable

For Each mySCTN In ActiveDocument.Sections
For Each myvar In ActiveDocument.Variables
If myvar.Value <> "" Then
With Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindContinue
.Execute FindText:=Trim(myvar.Value)
If .Found Then
Selection.Range.Font.Bold = True
End If
End With
End If
Next
Next
End Sub

TIA

Sophia
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Sophia > écrivait :
In this message, < Sophia > wrote:

|| Hello,
||
|| I have an rtf document open on Word97.
||
|| ActiveDocument.Sections.count =18
||
|| The sections are delimited by Section Breaks (Continuous)
||
|| I have, in the document, a list of words stored as Document
|| Variables.
||
|| I'm trying to find the first instance in each "Section"
|| of a word from that list and set the font for that "found" word
|| to "bold"... then go on through the list of words, setting any
|| other of the words from the list to bold ... then go on to the
|| next Section.
||
|| For some reason, instead of doing "For each Section", the
|| following code is doing "For each Paragraph" instead.
||
|| What am I doing wrong?
||
|| Public Sub aSetDocVarsToBold()
||
|| Dim mySCTN As Section
|| Dim oControl As Control
|| Dim i, SectionNumber
|| Dim myvar As Variable
||
|| For Each mySCTN In ActiveDocument.Sections
|| For Each myvar In ActiveDocument.Variables
|| If myvar.Value <> "" Then
|| With Selection.Find
|| .Forward = True
|| .ClearFormatting
|| .MatchWholeWord = True
|| .MatchCase = True
|| .Wrap = wdFindContinue
|| .Execute FindText:=Trim(myvar.Value)
|| If .Found Then
|| Selection.Range.Font.Bold = True
|| End If
|| End With
|| End If
|| Next
|| Next
|| End Sub
||
|| Sophia

The problem is that although you are iterating through each section, the
find is always done in the whole document because of:
|| With Selection.Find
Also, you are not telling the code to stop in a given section should the
word be found more than once. As is, your code will find all instances of
all words represented by the variables and bold all of them.
Using the range object gives you more control (You set the whole section as
the range in which to perform the find, if the text is found, the found text
becomes the new de facto range.) Also, using a slightly different syntax on
the conditional find (My "Do While ... Loop" bit ) helps leave the find loop
as soon as the first instance in the section range is found. Try the code
below.

'_______________________________________
Public Sub aSetDocVarsToBold()

Dim mySCTN As Section
Dim SecRange As Range
Dim myvar As Variable

For Each mySCTN In ActiveDocument.Sections
For Each myvar In ActiveDocument.Variables
If myvar.Value <> "" Then
Set SecRange = mySCTN.Range
With SecRange.Find
.Text = myvar.Value
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
Do While .Execute
SecRange.Font.Bold = True
Exit Do
Loop
End With
End If
Next
Next

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sophia

Jean-Guy ...

Sorry it took so long to respond, but my news server appears to have
taken a very long coffee break ...

Thank you again for your help, and for the explanation of my faux pas
de jour.

I am getting better at this as time goes on, but I have a bad habit of
scrounging in the microsoft.public.word.vba.* newsgroups and trying to
piece together things I find there ... without really taking the
trouble to analyze the monsters that I'm making.

The sheer number of For, Next, If and End If statements here got me
confused, and obviously I have to go back and learn more about the
Selection and Range objects. The terminology is also confusing, for
example that the Find property returns a Find object and that the Find
object has a Found property turns my head into a pretzel ...

Regards,

Sophia
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Sophia > écrivait :
In this message, < Sophia > wrote:

|| Jean-Guy ...
||
|| Sorry it took so long to respond, but my news server appears to have
|| taken a very long coffee break ...
||
|| Thank you again for your help, and for the explanation of my faux pas
|| de jour.

Not at all, we all make these kind of false assumptions, or have made them
before...

|| I am getting better at this as time goes on, but I have a bad habit of
|| scrounging in the microsoft.public.word.vba.* newsgroups and trying to

Excellent way to learn!

|| piece together things I find there ... without really taking the
|| trouble to analyze the monsters that I'm making.

Oooops, not so excellent anymore! ;-)

Use the step-by-step debugger to see the code in action. What I do is set my
VBA editor to one fifth of my screen height and I put it in front of my
document. I make sure that the part of the document on which the code acts
is visible in the top 4/5th.
Then I place my cursor anywhere in the sub I want to debug and I press F8.
Each F8 will execute one line of code. Thus you can see what is going on,
you can even move the code forward (skip some lines) or re-execute some.
Sometimes what I do is go to the document and undo the last action the code
has done (Or go see the impact a couple of pages away....), go back to the
code, edit the line it just executed, grab the arrow in the code margin (the
grey border on the left) that points at the line that will be executed next
and move it back next to the line I just edited and re-execute it to see the
impact of my modification.
Also, you can set break points, e.g. If you have a long loop and do not want
to step through it line by line by doing F8 one hundred times..., put a
break point by clicking in the margin (The grey border on the left) next to
the line after the loop exit and hit F5. The code will execute to the end,
or to the next break point. Then I continue with F8...

||
|| The sheer number of For, Next, If and End If statements here got me
|| confused, and obviously I have to go back and learn more about the
|| Selection and Range objects. The terminology is also confusing, for

If you plan on doing more coding, the Range object is a must.

|| example that the Find property returns a Find object and that the Find
|| object has a Found property turns my head into a pretzel ...

The Find properties are, if you want, the parameters that determine how the
find will be executed (forward, replace text or not, checking for case,
etc.) The Found property is just the result of the find... was it successful
or not. But as you saw with the example I posted, you can use the Execute to
achieve the same result.

Good luck!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sophia

Jean-Guy ...

Thank you for your kind advice ... especially the section below, which
I had never thought of. I had been switching back and forth between
full-screen copies of the document and the VB editor ... and just
trying to imagine what had been going on "behind the scenes."

Such a *great* idea ... and something I'd never seen in any of the
books I've read. It's one of those things in life that "should be
obvious," I suppose ... but wasn't for me, and I can't begin to tell
you how much of an asset it will be in the future ...

The idea of single-stepping through the code with everything
visible!!! Good grief!

It reminds me of the time I was in the supermarket, trying to get one
of those big leafy heads of lettuce into one of those diaphanous
plastic bags ... folding over the leaves (which were headed in all
directions) ... trying to manage the whole mess without breaking too
many ... and cussing to myself as I always did in that same situation
.... week after week.

There was a sprightly little old Chinese woman standing next to me,
looking at me as if I'd just landed there from another planet ... with
a gleam in her eye. She took the bag from me, put her right hand
*into* the bag, pulling the top edges in the direction of her elbow
.... then took the lettuce with her left hand and placed the root-end
into her bag-covered right hand ... then pulled the bag down, *over*
the lettuce without breaking a single leaf.

I stood there in complete amazement ... and when I finally got my
brain back in gear, I said to her, "How on earth did I live *all*
these years without thinking of *that*?" She responded with a
wonderful smile ... and without saying a single word ... went on her
way.

Since then, I've offered her idea to several others who I saw
struggling in the same situation ... and just a minute ago, I stored a
Google link to your message and will share it as often as I can with
others whose unmanageable code is hanging out in all directions like
mine was ...

Kind regards,

Sophia



Use the step-by-step debugger to see the code in action. What I do is set my
VBA editor to one fifth of my screen height and I put it in front of my
document. I make sure that the part of the document on which the code acts
is visible in the top 4/5th.
Then I place my cursor anywhere in the sub I want to debug and I press F8.
Each F8 will execute one line of code. Thus you can see what is going on,
you can even move the code forward (skip some lines) or re-execute some.
Sometimes what I do is go to the document and undo the last action the code
has done (Or go see the impact a couple of pages away....), go back to the
code, edit the line it just executed, grab the arrow in the code margin (the
grey border on the left) that points at the line that will be executed next
and move it back next to the line I just edited and re-execute it to see the
impact of my modification.
Also, you can set break points, e.g. If you have a long loop and do not want
to step through it line by line by doing F8 one hundred times..., put a
break point by clicking in the margin (The grey border on the left) next to
the line after the loop exit and hit F5. The code will execute to the end,
or to the next break point. Then I continue with F8...

<clip>
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Sophia > écrivait :
In this message, < Sophia > wrote:

|| Jean-Guy ...
||
|| Thank you for your kind advice ... especially the section below, which
|| I had never thought of. I had been switching back and forth between
|| full-screen copies of the document and the VB editor ... and just
|| trying to imagine what had been going on "behind the scenes."
||
|| Such a *great* idea ... and something I'd never seen in any of the
|| books I've read. It's one of those things in life that "should be
|| obvious," I suppose ... but wasn't for me, and I can't begin to tell
|| you how much of an asset it will be in the future ...
||
|| The idea of single-stepping through the code with everything
|| visible!!! Good grief!
||
|| It reminds me of the time I was in the supermarket, trying to get one
|| of those big leafy heads of lettuce into one of those diaphanous
|| plastic bags ... folding over the leaves (which were headed in all
|| directions) ... trying to manage the whole mess without breaking too
|| many ... and cussing to myself as I always did in that same situation
|| ... week after week.
||
|| There was a sprightly little old Chinese woman standing next to me,
|| looking at me as if I'd just landed there from another planet ... with
|| a gleam in her eye. She took the bag from me, put her right hand
|| *into* the bag, pulling the top edges in the direction of her elbow
|| ... then took the lettuce with her left hand and placed the root-end
|| into her bag-covered right hand ... then pulled the bag down, *over*
|| the lettuce without breaking a single leaf.
||
|| I stood there in complete amazement ... and when I finally got my
|| brain back in gear, I said to her, "How on earth did I live *all*
|| these years without thinking of *that*?" She responded with a
|| wonderful smile ... and without saying a single word ... went on her
|| way.
||
|| Since then, I've offered her idea to several others who I saw
|| struggling in the same situation ... and just a minute ago, I stored a
|| Google link to your message and will share it as often as I can with
|| others whose unmanageable code is hanging out in all directions like
|| mine was ...
||

Glad I could help!

If you are not already doing so, you should consider a carer in
writing.Really enjoy reading your story!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sophia

Jean-Guy ...

Thank you for the compliment about my writing. I don't know about a
career doing it ... I wouldn't know quite how to find my way in that
direction, but I do get a lot of practice in
microsoft.public.word.vba.beginners, trying to make myself understood.

I mean ... trying to explain what I'm attempting to accomplish in a
program when I get hopelessly tangled in it ... as I was at the
beginning of this thread. Often, just explaining it to myself gives
me the clues I need. But more often I end up here, trying to explain
how I tried to get my leafy head of lettuce in the plastic bag ... the
wrong way.

KRegards,

Sophia
 

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