Expected Function or Variable

S

Sandi V

I recorded a macro in Word 2003 that searches for a word and then applies a
character style to that word and to the following word. Then I found the
answer on these boards how to loop through the rest of the document. But
when I run the macro I get the "expected Function or Variable" error. Could
somebody please assist? Thanks in advance.
--Sandi


Sub SectionStyle()
'
' SectionStyle Macro
' Searches for the word Section and applies the Section character Style to
that word and the following word
'
With Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "section"
.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:=wdWord, Count:=2, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("section Char")
Loop
End With
End Sub
 
G

Gordon Bentley-Mix on news.microsoft.com

When it throws the error and you click Debug, which line of code is
highlighted? Is it the first With statement? Try commenting out this line and
the corresponding End With and see what happens. (Even if this doesn't fix
the problem, they appear to be superfluous to me and can be safely removed.)
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Greg Maxey

Gordon identified the first problem. Loop without Do will be next. Try:

Sub SectionStyle()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "section"
.Replacement.Text = ""
While .Execute
With Selection
.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend
.Style = ActiveDocument.Styles("section Char")
.Collapse wdCollapseEnd
End With
Wend
End With
End Sub
 
S

Sandi V

Thanks, guys! Hope you are both doing well!

Yes, I commented out the first With line and got past it like Gordon said I
would. As you predicted, then I got my error re: incorrect use of loop. I
got rid of my code and added yours. My next error is that the requested
member of the collection does not exisit. This is on the line that calls the
Style named Section -- which DOES exist. Any ideas of tht?

Sandi
 
G

Greg Maxey

Sandi,

The code calls for sytle named "section Char" is that what your section is
named or is just named "section?"
 
C

ChuckCohenDVM

I am looking for two macros for use in Word 2007. Hopefully someone may have
some knowledge of what is out there or be able to help me.

1) A macro that would let a reader of a document do a Find search for a
word. The result would not just highlight all the words ina adocument or
chapter or book. It would create a list or table of every instance of the
word, it's page and perhaps the closest section heading before it. So, if I
were searching for pancreatitis or pancrea I woudl be able to select a
listing, click on it and be taken to that location.

2) In my book I have 35-40 case hsitories, Case # < >. I would like to
have all instances of Cases # < > function like footnotes. So, if I start
to enter or insert a new case it is assigned the next case number. Esp. if I
insert a new case in the series of cases, the proper number would be assigned
to it and all cases after it would be properly renumbered.

Thanks much. I know I'm asking for a lot, but it is a real need.

Chuck Cohen
 

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