Solved

VBA to Set Caption ChapterStyleLevel property to custom style

Posted on 2009-07-08
11
638 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:WarCrimes
  • 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:WarCrimes
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
 
LVL 18

Author Comment

by:WarCrimes
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:WarCrimes
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:WarCrimes
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:WarCrimes
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now