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