We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

VBA to Set Caption ChapterStyleLevel property to custom style

Cory Vandenberg
on
Medium Priority
805 Views
Last Modified: 2012-05-07
Hello,

Is it possible to set the ChapterStyleLevel property to a custome style instead of one of the built in styles such as Heading 2?

I've tried the following, but it's not working, and the macro recorder in Word is about useless.

    With app.CaptionLabels("Table")
        .IncludeChapterNumber = True
        .ChapterStyleLevel = "Style Head 2"
    End With

Thanks for your help,

WC
Comment
Watch Question

Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
I don't believe you can use custom styles in this scenario.
Cory VandenbergSenior Risk Manager
CERTIFIED EXPERT

Author

Commented:
Ok, perhaps not directly at creation, but maybe once the caption is created.

I know you can right click the field in the caption and select Edit Field, then select the style that the REF is pointing to.

Is there a way to easily change the REF after creation, so that it is
{ STYLEREF \s "Style Head 2" }

instead of

{ STYLEREF 2 \s }

Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
Yes - there is a way to accomplish this AFTER the caption has been inserted.

1.  View field codes (Alt F9)
2.  Search and replace STYLEREF 2
3.  REPLACE WITH STYLEREF Style Head 2
4.  Select the field and press F9 to update
5.  Press Alt F9 to view result of field.

Cory VandenbergSenior Risk Manager
CERTIFIED EXPERT

Author

Commented:
Needs to be done with VBA
Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
OK - I'll give you a macro that does that....
Manager, Document Services Center
Top Expert 2004
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Cory VandenbergSenior Risk Manager
CERTIFIED EXPERT

Author

Commented:
Thank you for the example.

I'm coding this in Excel, so I am having to adapt it a bit.   I'm curious if there is a way to do the Find+Replace without using Selection.

Perhaps not.  I'll repost when I have finished adapting the code.

WC
Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
Nothing (no text) is selected during any of my macro - other than updating the fields .... that's just the reference Word VBA uses....
The first line goes to the beginning of the document....

But why in heaven's name would you code it in Excel???
Cory VandenbergSenior Risk Manager
CERTIFIED EXPERT

Author

Commented:
Ya i removed the first line as I don't need to move the insertion point.

I modified your example a bit, and fixed the relace string.  The following is working perfectly for me

    With wdApp
        .ActiveWindow.View.ShowFieldCodes = True
        With .Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "STYLEREF 2 \s"
            .Replacement.Text = "STYLEREF \s" & Chr(34) & "Style Head 2" & Chr(34)
            .Forward = True
            .Execute Replace:=wdReplaceAll
        End With
        .Selection.WholeStory
        .Selection.Fields.Update
        .ActiveWindow.View.ShowFieldCodes = False
    End With


I am coding it in Excel as an application I am updating creates a bunch of tables from flat files output by SAS, then generates Word document reports based on the data.  Basically, automating as much of report writing as we can.

I'm coding up custom styles to be used throughout the documents, and thus need to refer to these styles when labeling figures, tables, etc.

Thank you for the help.  Still learning my way around Word VBA.  I don't find the recorder there much use, as I can't do a lot of things once it's turned on, for instance right click a field.

Cheers,
WC
Cory VandenbergSenior Risk Manager
CERTIFIED EXPERT

Author

Commented:
Just needed some minor modifications, and I cleaned it up putting a lot of it in the With statement. (See below) Thanks for the help.
Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
Well, I'm glad you got it working the way you need it to work.  Thanks very much.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.