Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

picture captions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 07 Sep 2006 14:52 GMT
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
Cindy M. - 07 Sep 2006 15:42 GMT
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?
[quoted text clipped - 7 lines]
> 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 :-)
Mike - 07 Sep 2006 16:23 GMT
Cindy, Thanks for the info.

Mike.
> Hi Mike,
>
[quoted text clipped - 42 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Mike - 07 Sep 2006 16:47 GMT
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.

> Hi Mike,
>
[quoted text clipped - 42 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Cindy M. - 08 Sep 2006 10:54 GMT
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
[quoted text clipped - 17 lines]
> 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 :-)
Mike - 08 Sep 2006 15:48 GMT
Thanks!

> Hi Mike,
>
[quoted text clipped - 45 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.