Re: Help With Code

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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"
>
.



Relevant Pages

  • Re: Help With Code
    ... The macro recorder always uses the selection object, ... then this returns true, even if you modified the paragraph, ... and avoids formatting of the space by setting up a temporary ...
    (microsoft.public.word.vba.general)
  • Re: Help With Code
    ... am I correct if I would say you use Dim ... object (like the name of a paragraph style in your document)? ... > Greg Maxey/Word MVP ... >>> which may lead beginners to stick to the selection. ...
    (microsoft.public.word.vba.general)
  • Re: Moving Styles in a script
    ... Word already has a way to apply styles to the selection. ... your add-in. ... or the selected paragraph will be formatted other than as ... clikcs on a toolbar button which initiates the macro. ...
    (microsoft.public.word.formatting.longdocs)
  • Re. Module Comments
    ... comment between Sub or Function, ... to divide a written paragraph into lines of adequate length. ... 'cursor after the last character of the selection ... 'selection of the first character in the line ...
    (microsoft.public.excel.programming)
  • Re: Word 2000 Styles not in Word2007 Styles list
    ... If I'm correctly interpreting what you're asking, I think you have to set the priority of your preferred styles to be higher than that of the others in the Quick Style gallery. ... As for updating styles, if you make a change to a single paragraph in a given style and want to update the style to include that change, you can use Update Style to Match Selection. ...
    (microsoft.public.word.docmanagement)