Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Macro applies wrong style?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gary Fritz - 05 Jul 2007 23:55 GMT
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
Russ - 06 Jul 2007 02:24 GMT
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?

> 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
[quoted text clipped - 25 lines]
> Any guesses why it refuses to apply the Code style??
> Gary

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Shauna Kelly - 06 Jul 2007 08:08 GMT
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

>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
[quoted text clipped - 28 lines]
> Any guesses why it refuses to apply the Code style??
> Gary
Gary Fritz - 06 Jul 2007 20:56 GMT
Russ <drsN0SPAMmikle@hotmailD0Tcom.INVALID> wrote:
> 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.

> 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
Klaus Linke - 07 Jul 2007 00:36 GMT
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
Gary Fritz - 07 Jul 2007 17:56 GMT
> 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
Klaus Linke - 07 Jul 2007 19:10 GMT
Sounds like my suspicion was wrong. You could mail me a sample doc
(info@fotosatz-kaufmann.de) if you want...

Regards,
Klaus
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.