Re: Help With Code
- From: Fred Goldman <FredGoldman@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 22 Jan 2006 07:16:03 -0800
Helmert,
Thank you very much, while Greg gave me working code (thank you, too), you
answered a few of the questions I really wanted to know.
Just a couple more questions:
1) I've noticed both you are setting variables with "Dim As", what exactly
is the difference between "Set" and "Dim As"?
2) What does "Case" mean?
3) I've noticed a lot of people say that they don't want the space after a
word tagged with a character style, what is wrong with that?
"Helmut Weber" wrote:
> Hi Fred,
>
> try to get used to range-objects.
> The macro recorder always uses the selection object,
> which may lead beginners to stick to the selection.
>
> Most often it is not necessary to use selection,
> it's also slow and needs too much code to control.
>
> Use "Option explicit"
> see: Tools, Options, Editor, Require Variable Declaration.
>
> >Set myPara = ActiveDocument.Styles("Body Text2")
> This is probably not what you want.
> You are creating a style object, IMHO,
> >if Selection.Style = myPara
> will return false if you modified a paragraph directly,
> by e.g. defining a right indent,
> as all of the style of the selection is not equal
> to all of the style of the template.
>
> If you use selection.style like
> if selection.style = "Normal",
> then this returns true, even if you modified the paragraph,
> as it checks only for the name of the style, not for all of it.
>
> >How would I set myPara to more than one style?
>
> No way. You have to use "or" or "select case".
>
> if selection.style = "Normal" or selection.style = "Normal2"
>
> or:
>
> Sub test500()
> Dim oPrg As Paragraph
> For Each oPrg In ActiveDocument.Paragraphs
> With oPrg.Range.Words(1)
> .Select ' for testing using F8, delete after testing!
> Select Case oPrg.Style
> Case "Normal": .Style = "ChStyle1"
> Case "Heading 2": .Style = "ChStyle2"
> Case "Heading 3": .Style = "ChStyle3"
> End Select
> End With
> Next
> End Sub
>
>
> Greg's code takes care in addition of the fact,
> that an ordinary word in Word may encompass a trailing space,
> and avoids formatting of the space by setting up a temporary
> range, moving the end of the range to the left, and applying
> the formatting to the so shortened range.
>
> Sure, lots of questions left...
>
> HTH
>
> --
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
>
.
- Follow-Ups:
- Re: Help With Code
- From: Greg Maxey
- Re: Help With Code
- From: Helmut Weber
- Re: Help With Code
- References:
- Re: Help With Code
- From: Helmut Weber
- Re: Help With Code
- Prev by Date: Returning a collection from a function.
- Next by Date: Re: Returning a collection from a function.
- Previous by thread: Re: Help With Code
- Next by thread: Re: Help With Code
- Index(es):
Relevant Pages
|