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
find a caption
-
- Panoramic Lounger
- Posts: 8266
- Joined: 25 Jan 2010, 09:09
- Location: retirement
-
- Administrator
- Posts: 12679
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: find a caption
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
-
- Panoramic Lounger
- Posts: 8266
- Joined: 25 Jan 2010, 09:09
- Location: retirement
Re: find a caption
No, sophistication is beyond my limited VBA skills so that will do nicely.
![ThankYou :thankyou:](./images/smilies/thankyou.gif)
Ken
![ThankYou :thankyou:](./images/smilies/thankyou.gif)
Ken
-
- 4StarLounger
- Posts: 508
- Joined: 17 Dec 2010, 03:14
Re: find a caption
You don't even need VBA for this - a simple Find will do...
Paul Edstein
[Fmr MS MVP - Word]
[Fmr MS MVP - Word]
-
- Panoramic Lounger
- Posts: 8266
- Joined: 25 Jan 2010, 09:09
- Location: retirement
Re: find a caption
Not I my context, the macro I'm constructing needs to be able to find Captions, I needed example code to get me started.macropod wrote:...a simple Find will do...
Ken
-
- 4StarLounger
- Posts: 508
- Joined: 17 Dec 2010, 03:14
Re: find a caption
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]
[Fmr MS MVP - Word]
-
- Panoramic Lounger
- Posts: 8266
- Joined: 25 Jan 2010, 09:09
- Location: retirement
Re: find a caption
macropod wrote:...select the first Table caption, ignoring all others...
![ThankYou :thankyou:](./images/smilies/thankyou.gif)
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 :grin:](./images/smilies/grin.gif)
Ken
-
- Administrator
- Posts: 78920
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: find a caption
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.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 641
- Joined: 10 Jan 2016, 15:56
- Location: Madison, Wisconsin
Re: find a caption
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.
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.
![Cheers :cheers:](./images/smilies/cheers.gif)
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word