Solved

VBA to Set Caption ChapterStyleLevel property to custom style

Posted on 2009-07-08
11
653 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
0
Comment
Question by:Cory Vandenberg
  • 6
  • 5
11 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24804044
I don't believe you can use custom styles in this scenario.
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24804333
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 }

0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24804786
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.

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24805481
Needs to be done with VBA
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24805589
OK - I'll give you a macro that does that....
0
 
LVL 37

Accepted Solution

by:
Joanne M. Orzech earned 500 total points
ID: 24805629
   Selection.HomeKey Unit:=wdStory
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "STYLEREF 2"
        .Replacement.Text = "STYLEREF Style Head 2"
        .Forward = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.WholeStory
    Selection.Fields.Update
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24805840
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24805993
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???
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24806166
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
0
 
LVL 18

Author Closing Comment

by:Cory Vandenberg
ID: 31601120
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.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24806197
Well, I'm glad you got it working the way you need it to work.  Thanks very much.

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question