picture captions

M

Mike

Hi there,

I'm relatively new using VBA in Word2k, and wondering how I can find (in my
application) the captions of all the pictures that are embedded?

i.e. I have screenshots stuck in my document, and I have added captions
(using the insert/caption... command). I would like to be able to
programmatically go through the document, find all the pictures and add
captions (I inherited the document with the pictures already in, or else I
could use the autocaption feature) and then I'd like to be able to go
through and edit the captions through VBA. I know I can find all the
pictures by going through the document.shapes collection, but how do I
identify if there is a caption attached and select the caption?

TIA,

Mike
 
C

Cindy M.

Hi Mike,
I'm relatively new using VBA in Word2k, and wondering how I can find (in my
application) the captions of all the pictures that are embedded?

i.e. I have screenshots stuck in my document, and I have added captions
(using the insert/caption... command). I would like to be able to
programmatically go through the document, find all the pictures and add
captions (I inherited the document with the pictures already in, or else I
could use the autocaption feature) and then I'd like to be able to go
through and edit the captions through VBA. I know I can find all the
pictures by going through the document.shapes collection, but how do I
identify if there is a caption attached and select the caption?
There's nothing in Word that links a caption to any particular graphic. And
where a caption can be found depends on what the text flow formatting was
when Insert/Caption was invoked (in-line or not).

You mention the Shapes collection. If all the pictures are formatted with
text-flow (not "in-line") and you want to find any captions, those will be in
a document.Shapes(n).Type = msoTextBox. In this case, the
document.Shapes(n).TextFrame.TextRange will contain a field of the type
wdFieldSEQ, and the SEQ field will contain the caption lable ("Figure", for
example).

Or, to put it another way:
- you can loop through the Shapes and check the Type
- if you pick up a Type msoTextBox, then this could be a caption
- You get the text in the box through s = shape.TextFrame.TextRange.Text
- You can check whether it contains fields: TextFrame.TextRange.Fields.Count
- If it does, you can check the Type of each field (wdFieldSEQ)
- You check whether the Field.Code string contains the caption label you're
interested in
- If it does, then you can pick up the range following the field result and
assume it contains the caption. This can be edited, replaced, etc.

It might be faster to just read through the document and make the changes as
you go...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Mike

Cindy, Thanks for the info.

Mike.
Cindy M. said:
Hi Mike,

There's nothing in Word that links a caption to any particular graphic. And
where a caption can be found depends on what the text flow formatting was
when Insert/Caption was invoked (in-line or not).

You mention the Shapes collection. If all the pictures are formatted with
text-flow (not "in-line") and you want to find any captions, those will be in
a document.Shapes(n).Type = msoTextBox. In this case, the
document.Shapes(n).TextFrame.TextRange will contain a field of the type
wdFieldSEQ, and the SEQ field will contain the caption lable ("Figure", for
example).

Or, to put it another way:
- you can loop through the Shapes and check the Type
- if you pick up a Type msoTextBox, then this could be a caption
- You get the text in the box through s = shape.TextFrame.TextRange.Text
- You can check whether it contains fields: TextFrame.TextRange.Fields.Count
- If it does, you can check the Type of each field (wdFieldSEQ)
- You check whether the Field.Code string contains the caption label you're
interested in
- If it does, then you can pick up the range following the field result and
assume it contains the caption. This can be edited, replaced, etc.

It might be faster to just read through the document and make the changes as
you go...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Mike

Cindy,

after reading your reply and trying some of what you suggested, I also tried
(as I discovered after your reply that my pictures fall in to the
inlineshapes collection)

activedocument.Range( _
activedocument.InlineShapes(1).Range.End+1, _
activedocument.InlineShapes(1).Range.End+2) _
.Words(1).Next(wdword, 1).Fields(1).Code
which would return the code of the field in the word unit AFTER the word
that occupies the point 1 character after the picture, i.e., it finds the
picture, then gets the word that exists immediately after the picture, and
then gets the NEXT word after than, and returns the code of the field that
makes up that word.

If that "NEXT" word isn't a field, then I'd get an error, otherwise I'd
check (as per your suggestion) if the field contains the caption label I'm
looking for (I guess I could add in a check to see if the word immediately
after the picture is also the same as the caption label)

Does that seem like a reliable solution for my problem, or is there a
potential issue in there?

Mike.
 
C

Cindy M.

Hi Mike,
after reading your reply and trying some of what you suggested, I also tried
(as I discovered after your reply that my pictures fall in to the
inlineshapes collection)

activedocument.Range( _
activedocument.InlineShapes(1).Range.End+1, _
activedocument.InlineShapes(1).Range.End+2) _
.Words(1).Next(wdword, 1).Fields(1).Code
which would return the code of the field in the word unit AFTER the word
that occupies the point 1 character after the picture, i.e., it finds the
picture, then gets the word that exists immediately after the picture, and
then gets the NEXT word after than, and returns the code of the field that
makes up that word.

If that "NEXT" word isn't a field, then I'd get an error, otherwise I'd
check (as per your suggestion) if the field contains the caption label I'm
looking for (I guess I could add in a check to see if the word immediately
after the picture is also the same as the caption label)

Does that seem like a reliable solution for my problem, or is there a
potential issue in there?
I guess it would work, although I might approach it differently... In my
experience, most captions are not in the same line/paragraph as the picture.
If that's the case, here, then
Dim rng as Word.Range
Set rng = ActiveDocument.InlineShapes(1).Paragraphs(1).Range
rng.Collapse wdCollapseEnd 'should now be in the next para
If rng.Fields.Count > 1 Then
theFieldCode = rng.Fields(1).Code
If Instr(theFieldCode, "SEQ") <> 0 AND
rng.Style = wdStyleCaption THEN
'I'm almost certainly in a caption paragraph

Ah, and something that didn't occur to me, yesterday (but would be a bit more
difficult when things are in textboxes, anyway): You could do a "FIND" on the
Caption style. Of course, the user may have goofed and used the style
elsewhere in the document, as well, so in the end you'd probably have to check
the field code, too. And "FIND" tends to be a bit slow in large documents.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Mike

Thanks!

Cindy M. said:
Hi Mike,

I guess it would work, although I might approach it differently... In my
experience, most captions are not in the same line/paragraph as the picture.
If that's the case, here, then
Dim rng as Word.Range
Set rng = ActiveDocument.InlineShapes(1).Paragraphs(1).Range
rng.Collapse wdCollapseEnd 'should now be in the next para
If rng.Fields.Count > 1 Then
theFieldCode = rng.Fields(1).Code
If Instr(theFieldCode, "SEQ") <> 0 AND
rng.Style = wdStyleCaption THEN
'I'm almost certainly in a caption paragraph

Ah, and something that didn't occur to me, yesterday (but would be a bit more
difficult when things are in textboxes, anyway): You could do a "FIND" on the
Caption style. Of course, the user may have goofed and used the style
elsewhere in the document, as well, so in the end you'd probably have to check
the field code, too. And "FIND" tends to be a bit slow in large documents.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top