Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use application of style sheet to trigger macro

Status
Not open for further replies.

hubbiida

Technical User
Jul 9, 2003
5
US
Hi All,
I'm rather a novice at VBA, but I have to create some basic macros for a client in Word. What I need to do that I can't figure out is insert some text automatically whenever a certain style sheet is applied. Inserting the text is a breeze, but I can't figure out what sort of If statement (if that is what is called for) might connect application of the style to running the code to insert the text. Below is one attempt, it may show the direction I'm going in (remember, I have absolutely no idea what I'm doing...)
Many thanks in advance

If Style = "ANNEX" Then
Selection.Style = ActiveDocument.Styles("ANNEX")
Selection.TypeText Text:=Chr(11)
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="(Informative)" & Chr(11) & Chr(11)
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="Annex Title"
End If
End Sub
 
Style sheet" is not a correct or accurate term with Word.

OK, if I understand correctly, you are asking for some sort of logic that if a spoecific stule is applied, then insert specific text. Hmmmm, a bunch of questions (and statements) come to mind.

1. What is the logic that determines when the style is applied?

2. Does this means EVER TIME that style is used you want the text to be inserted?

3. Style can be turned on at an Insertion point (that is, the Selection is collapsed to a single character), or it can be applied to existing text. That existing text can have a style applied to it by making it a Range, or a Selection. Ranges are better.

4. Why are you using the double Chr(11)? If you are using them to make vertical space between text....go back to Start. You are using Styles incorrectly.

Gerry
 
The logic is that when the style is applied at an insertion point the text will be inserted and will have the style applied to it. If possible, I would rather not have the text inserted if the sytle is applied to a selected range, but only at an insertion point.

I am using double Chr(11)because the lines need to appear as a single line in a TOC field and I do not want any paragraph spacing before the first line.

Thanks much for your help.
Andrew
 
The logic is that when the style is applied at an insertion point the text will be inserted and will have the style applied to it.

Sorry, but you need something that will fire this then.

Whatever you use to fire your code will need to check if the Selection is, in fact, collapsed and is just an Insertion point. Use:
Code:
If Selection.Type = wdSelectionIP Then
[COLOR=red]   ' Selection IS an insertion point
   ' do your stuff[/color red]
Else
  [COLOR=red]' Selection is NOT insertion point
   ' do something else, Exit Sub??[/color red]
End If

If the Selection is collapsed to an insertion point, you are not really applying a style. You are toggling it. You are turning the style on. Any text insertes afterwords will have that style. Take care here, because are you using a paragraph style, or a character style?

You still need to answer the question - do you want this to fire EVERY TIME the style is used? You did not answer this.
I am using double Chr(11)because the lines need to appear as a single line in a TOC field and I do not want any paragraph spacing before the first line.

This is sounding a little hairy.

Could you please describe EXACTLY what you are doing? It sounds like you want to place the insertion point in a TOC field, and insert text in a particular style.

Is this correct? If so, this is a very strange thing to do.

Again though....what is going to fire this code? What makes it happen? If you are trying to trap simply changing the style - that is, if someone goes to the Style dropdown, and selects this style - if you are trying to trap/catch that event, then you need to use Application events.

However, if you want code to actually do this, on your command, then that is fairly straightforward.

"when the style is applied at an insertion point" is the key here. When DOES it? What event applies the style? You do? Whenever anyone, any time, applies the style?

Gerry
 
Turning on the style after the insertion point (togglling it) is fine, because then the inserted text will, I assume, adopt that style.

The style is a paragraph style.

The thing I would like to trigger the insertion of the text is exactly what you describe: "if someone goes to the Style dropdown, and selects this style "

I DO absolutely want this to fire every time the style is used.

The style of the inserted text is designated as a level in an auto-generated TOC (i.e., a field?). Any text to which that style is applied will appear in the TOC. The insertion point WILL NOT be placed in a TOC. The inserted text must have soft returns or each line will appear as a separate line in the TOC, where in fact they must appear as one single line. (This aspect of it works perfectly fine alread, and in fact is not my design, but is the design of the people who originally created the template which I must base my work on, which is ISO... so it must be that way...)

Thanks Gerry, I really appreciate your taking the time to help.
Andrew
 
That does not make sense.

Say you can trap the event of someone clicking on the Style dropdown, and selecting a specific style. You are then going to have that event - selecting a styule - insert text.

OK. So I select the Style 123 times. I get 123 instances of the same text. That does not sound right. Are you planning to pass the text in as a parameter?

What version of Word are you using? I am having trouble getting me head around this. Why not just use a specific style for the generation of the ToC? Then whatever text you want in the ToC, make it that style.

A further point is this: making text a style - then manually formatting it is an abuse of styles. You have text you make a style, then make some of the text bold, then some not. There is no point in using a style if you ignore its attributes.



Gerry
 
Hey, you know what pal, I figured this out on my own. So thanks for your help...but your 'tude, well, it's a bit rude, so, toodaloo to you...
 
Hi Andrew,

I'm glad you're sorted but would you be so kind as to post some details of what you've done to get sorted. I don't think I understand exactly what you wanted and seeing the solution would help me, and probably those who come to this later.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top