r/vba•
tslime

How can I execute a macro in Word 2003 whenever I apply a particular style?

Discussion

Title says it all really I just have a simple macro that inserts a SEQ field I want that to run whenever I apply a particular formatting style, any ideas? You'd have thought someone would have wanted this at some point but I guess not, nothing on google anyway.

Any help much appreciated!

Posted Aug 11, 2021, 11:17 AM UTC

33
joelfinkle2•

I don't think it can be done on the event of the style changing, because there's no such event to hook.

Your best bet is to create a macro that applies the style and inserts the field, and make a button for it.

2
Donny_Do_Nothing•

u/joelfinkle is right. You don't want vba constantly monitoring to see if you apply a specific style. Using a macro to apply the style is the way to go.

I can see it being a pain to remember to use a macro button for that particular style and not the others so you might just make a button for each style you use and create a toolbar for applying styles. That way, if you decide to add any other actions to the other styles, you've already got the framework set up.

If you want it to be even more flexible, you could forgo the styles and encode the formatting directly into the macro. Then you could export the thing as an addin and never have to worry about whether a document has those styles in it. Just food for thought.

1
tslimeOP•

Ok cheers bud

1