Solved

VBA to Set Caption ChapterStyleLevel property to custom style

Posted on 2009-07-08
11
665 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error 5180 Word Cannot open document template 1 41
Convert .PDF 6 68
Surface Book vs Surface Pro 4 86
Word 2010 - Review Pane setting is not persisting 4 24
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

828 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