Macro applies wrong style?

G

Gary Fritz

I wanted a macro to apply a common format that I use in my work. No
problem, I started recording a macro, assigned it to a key, then used the
Style pulldown to select "Code". It changed to Courier font and Code style
like it should. I ended the recording, and tried applying it. It changed
the style to "Caption" !

I tried recording macros using other styles, and they worked fine. I tried
recording macros while I applied the Code style to highlighted text, and to
an entire paragraph. Both of them changed to Caption.

The macros look like this:

Sub GF_CodeFont()
'
' GF_CodeFont Macro
' Macro recorded 7/5/2007 by Gary Fritz
'
Selection.Style = ActiveDocument.Styles("Code Char")
End Sub
Sub GF_CodePara()
'
' GF_CodePara Macro
' Macro recorded 7/5/2007 by Gary Fritz
'
Selection.Style = ActiveDocument.Styles("Code")
End Sub

Any guesses why it refuses to apply the Code style??
Gary
 
S

Shauna Kelly

Hi Gary

What is your overall aim here? Is your aim to have a keyboard shortcut
combination that invokes a certain style? If so, then you don't need a
macro. Instead, do this:

1. Tools > Customize.

2. On the Commands tab, the Save In should say "Normal.dot" if you want your
shortcut to be available in all the documents you work on.

3. On the Commands tab, click Keyboard.

4. In the "Customize Keyboard" dialog box, in the Categories list, choose
Styles. In the Styles list choose your style. Click in the "Press new
shortcut key" box, and then type your shortcut. Word will warn you if you
choose a keyboard combination that has already been assigned to something
else.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Gary Fritz

Russ said:
http://www.bettersolutions.com/word/WXX433/LI116411916.htm
According to this website it matters whether a style is a paragraph or
character style. Can you find your answer here?

Thanks, but no. I knew about the paragraph vs. character style. That's
why I tried the macro both ways.

Shauna Kelly said:
What is your overall aim here? Is your aim to have a keyboard shortcut
combination that invokes a certain style? If so, then you don't need a
macro. Instead, do this:

That is indeed my aim. I used Tools > Customize > Keyboard to assign the
macro to a key. Didn't realize you could do the same thing with a Style!

However: I assigned "Code Char" to a key, selected some text, hit the key,
and... it set it to "Caption" !

I appear to have some kind of mixup between my "Code" / "Code Char" and
"Caption" styles. These styles come with a template I use when doing work
for my main client. If I apply Code directly, it works as intended: it
results in a Courier font condensed by 1pt, in addition to a few other
tidbits. But if I apply it via macro or key assignment, it assigns the
wrong style. Weird.

Gary
 
K

Klaus Linke

Hi Gary,

Open the VBA editor (Alt+F11), then
open the immediate window (Ctrl+G).

Type in
? ActiveDocument.Styles("Code Char").LinkStyle

and hit the Return key at the end of the line.

The "Char" bit in the style name indicates it's a linked character style,
and it should be linked to the "Code" paragraph style.

If it's linked to the "Caption" style, something in Word went wrong. It's
usually called "corruption", but with these linked styles, things go wrong
so often it's more of a "feature"... :-(

You might be able to fix it:
ActiveDocument.Styles("Code Char").LinkStyle=ActiveDocument.Styles("Code")
.... but it might well mess with your formatting.

It might be better to search for "Code Char", and fix it by applying some
sensible (unlinked) character style.

Regards,
Klaus
 
G

Gary Fritz

Klaus Linke said:
Type in
? ActiveDocument.Styles("Code Char").LinkStyle
and hit the Return key at the end of the line.

At first I got Error 5941: The requested member of the collection does
not exist. (??) But I copied/pasted it again and this time I got
"Code".
The "Char" bit in the style name indicates it's a linked character
style, and it should be linked to the "Code" paragraph style.

I believe it is, If you select "Code" while the cursor is in a
paragraph, it sets the para to "Code". If you select "Code" while text
is selected, it changes only the selected text.
You might be able to fix it:
ActiveDocument.Styles("Code
Char").LinkStyle=ActiveDocument.Styles("Code") ... but it might well
mess with your formatting.

Sounds like "don't do this if you don't understand what you're doing" ...
which I don't. But since the LinkStyle is correct, I shouldn't need to
do this anyway, right?
It might be better to search for "Code Char", and fix it by applying
some sensible (unlinked) character style.

Code Char is Command Char + condensed by 1pt.
Command Char is Default Paragraph Font + Font: (Default) Courier New,
English U.S., Kern at 10pt. (It also says (Complex) Arabic Saudi Arabia
but I have NO idea where that came from...)

I could just apply a slightly modified Courier New, but other than this
problem, Code has always worked fine. And it's what I'm "supposed" to
use.

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top