PDA

View Full Version : Event When Paragraph Style Selected



f2e4
06-15-2012, 06:12 AM
I have created a template with a variety of custom styles.

When I select a paragraph that has the default "Normal" style I want to change it to one of my custom styles "MM_Action_Type_2". This style simply indents the text and shades it blue.

I have trying find VBA code that will activate as soon as I change the "Normal" style paragraph to a "MM_Action_Type_2" style paragraph. The change I am looking for is, as soon as the style is selected, I want the code to insert the following text, "Action Type 2:" (in bold text).

I think the following code will work but without the text being bold, but I'm not sure how to get the event to run every time I select the "MM_Action_Type_2" style:

Sub Action_Type_2()

Application.ScreenUpdating = False

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("MM_Action_Type_2")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "Action Type 2:"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Application.ScreenUpdating = True

End Sub

Question 1: Will the above code change only the paragraph that I have selected and not all instances that have already been given this style?

Question 2: Is there a way of checking to see if the text has already been added and, if so, not to run the code?

Question 3: How easy is it to make the inserted text bold but not change the rest of the paragraph?

Any help is appreciated.

Frosty
06-15-2012, 09:23 AM
For a VBA beginner, you have certainly attempted to tackle one of the more difficult concepts: Events.

The short answer to your questions:
1. No, it will change all instances... hence the "replace all" part.
2. Sure there is
3. Easy

However, that said... I think you need to define how you want this to work.

1. Is this for you, or are you going to be giving this to other people?

2. Since you talked about having something happen automatically (and forgetting about actual Word events-- you're not ready for that discussion yet, since technically what you're asking is possible, but it's not worth it), I think what you need is simply a better user interface for doing what you want to do.

3. What version of Word are you using?

4. Did you find the code above, or did you get it by recording a macro? Recording a macro is really the best way to learn VBA for a beginner with no programming background. You would have been able to answer your own questions very easily. Try recording a macro to do what you want (highlight a paragraph, apply a style, hit the home key to put your cursor at the beginning of the paragraph, type some text).

When you record simple macros and then read the resulting vba code, it's a great "rosetta stone" for knowing which actions did which bit of vba code.

f2e4
06-16-2012, 09:40 AM
Frosty,

Thanks for the reply. Responses to your questions as follows:

1. The code is to be added to a template that will be used by up to 10 people.

2. I'm using Word 2007

3. I found the code scouring the net to see if there were alternative ways of coding this task.

I do agree that recording macros does indeed help you pick up VBA much quicker or at least it did for me with Excel. VBA for Word seems a bit Alien for me whereas Excel was more logical.

I'll try recording a macro when i'm back in the office and see if I can figure this one out.

I'll post the resulting code so for any other interested parties.