Re: Help With Code

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



Fred,

I am not ignoring you. I simply don't feel comfortable trying to provide a
technical explanation to your question. I am a novice and dabbler in VBA
with no formal or professional experience.

It is a little like when I want to draw with a black crayon I pick up a
black crayon. I don't know why it is black I just know it is black ;-)

Since your question is buried several levels deep in this thread, you might
want to post as a new question in a day or so if someone doesn't come in
here to answer.

Sorry I couldn't make it all clear. To tell you the truth there is plenty
in VBA that is still a mystery to me.




--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Fred Goldman wrote:
> Scratch that last question. Rather, am I correct if I would say you
> use Dim to name a Object as a variable and Set when you want to use a
> part of a object (like the name of a paragraph style in your
> document)?
>
> "Greg Maxey" wrote:
>
>> Fred,
>>
>> Yes, I neglected to answer your last question. There is nothing
>> wrong with it if it is what you want. Say the character style was
>> "underlined." Personally I think it would look odd to underline the
>> space after.
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>>
>> Fred Goldman wrote:
>>> 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"


.