find a caption

User avatar
stuck
Panoramic Lounger
Posts: 8264
Joined: 25 Jan 2010, 09:09
Location: retirement

find a caption

Post by stuck »

With a bit of luck I'll find the answer with my next Google search but meanwhile...

I want some code to find a caption in my document, specifically a Table. It will be styled correctly with the caption style. Any suggestions gratefully received.

Thanks,

Ken

User avatar
StuartR
Administrator
Posts: 12678
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: find a caption

Post by StuartR »

Presumably you mean something more sophisticated than

Code: Select all

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Caption")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
StuartR


User avatar
stuck
Panoramic Lounger
Posts: 8264
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: find a caption

Post by stuck »

No, sophistication is beyond my limited VBA skills so that will do nicely.

:thankyou:

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: find a caption

Post by macropod »

You don't even need VBA for this - a simple Find will do...
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8264
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: find a caption

Post by stuck »

macropod wrote:...a simple Find will do...
Not I my context, the macro I'm constructing needs to be able to find Captions, I needed example code to get me started.

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: find a caption

Post by macropod »

In that case, you can even differentiate between caption types. For example, the following will select the first Table caption, ignoring all others:

Code: Select all

Sub Demo()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .Style = "Caption"
    .Text = "^d SEQ Table"
    .Replacement.Text = ""
    .Wrap = wdFindContinue
    .MatchWildcards = False
    .Execute
  End With
    If .Find.Found Then .Paragraphs(1).Range.Select
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8264
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: find a caption

Post by stuck »

macropod wrote:...select the first Table caption, ignoring all others...
:thankyou:

and by extrapolation I could tweak the line
.Text = "^d SEQ Table"
to say
.Text = "^d SEQ Figure"
and find the first Figure, correct? No need to answer though, I'll experiment and learn by my mistakes :grin:

Ken

User avatar
HansV
Administrator
Posts: 78917
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find a caption

Post by HansV »

As you probably expected, you can use each of the labels defined for captions. The labels Equation, Figure and Table are built-in, but it works the same for custom labels defined by clicking 'New Label...' in the Caption dialog.
S115.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: find a caption

Post by Charles Kenyon »

Just one other thought...
The macro invokes regular (advanced) find capabilities. If this is something you normally need to do on a frequent basis, a macro is fine. Otherwise, you'll be ahead of the game simply learning to use the find dialog. To get to styles, you need to click on the More button and then Format.