Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA to Set Caption ChapterStyleLevel property to custom style

Posted on 2009-07-08
11
Medium Priority
?
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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 …
Suggested Courses

636 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