Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AutoCaption With the Filename of the Image

2,730 views
Skip to first unread message

David

unread,
Jul 9, 2008, 4:58:17 PM7/9/08
to
(The real "meat" of my question is in the last paragraph, so please skip
ahead to it if you don't want to read all of this)

I'm currently working on a document that will use a lot of figures. At this
point I'm ready to start inserting the figures and I've been trying to figure
out the most efficient way to do this. What I've tried so far is to create a
Building Block called InsertFigure that contains two other Content Controls;
Picture and a custom InsertFigureAutoCaption control that automatically
numbers the figure and prompts the user to give a title for the caption. So
when I insert a figure using the InsertFigure QuickPart, a dialogue box comes
up prompting the user for the title, and when entered a properly labeled
placeholder comes up with all the correct formatting and the only thing I
have to do is click Change Picture and boom, exactly what I want. Well,
almost. There are a few problems.

When I created the Building Block I wasn't sure of the size I wanted for the
image, so all the figures are the same, too-small size. I want to make them
larger now and the only way I've found to do that is to go through 1 by 1. I
should also mention that I want them all the same size and I did some
searching for a way to easily resize all the images at once and found a macro
that someone had coded. I tried it and it screwed a lot of stuff up. So I
ditched that.

Another problem is that I don't like the Picture border I originally chose
for the Building Block and want to change it. Inside of the InsertFigure
building block I formatted the Picture content control to behave according to
the Picture style. I though I'd want a fancy Word 2007 Picture border and
didn't want to use a dull Word 2003 Paragraph border. Well my tastes have
changed and found that it not only looks better but is much easier to change
if I use the Picture style to control the Picture border. I guess I'm
wondering how can I change all of the InsertFigure bulding blocks I've
inserted to completely get rid of the Picture formatting border?

Now this is what I really want to know... After doing some searching I see
that it's possible to "mail merge" images into a document. So this got me
thinking: Is it possible to create some sort of AutoText (or something of the
like) entry and use field codes to insert an image from a particular database
or more preferably a common folder? The images would have to be inserted in
a particular order from the top of the document to the bottom. If the mail
merge is too complicated or not possible, then is it possible to create a
custom Building Block to insert an image with a field code just below it so
that the field code automatically creates a caption that gives the figure
number AND the filename of the image (without the path or the .jpg
extension). For instance I'd like to create a folder with all of the images
I'm going to use that have a filename similar to "2.4 - Picture of a
Cat.jpg". If I use a custom QuickPart, say "InsertFigure," then I'd want the
image to come up with a caption below it that says, "Figure 2.4 - Picture of
a Cat."

--Any suggestions, ideas, experiences, etc, is certainly appreciated. Sorry
for "rambling." Thanks in advance.

David

unread,
Jul 9, 2008, 5:16:16 PM7/9/08
to
I am using Word 2007 by the way.

Suzanne S. Barnhill

unread,
Jul 9, 2008, 6:13:03 PM7/9/08
to
Yeah, your reference to Building Blocks gave that away. <g>

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

"David" <Da...@discussions.microsoft.com> wrote in message
news:FB1DD05B-5E06-4148...@microsoft.com...

Doug Robbins - Word MVP

unread,
Jul 9, 2008, 7:16:15 PM7/9/08
to
I posted this for someone else the other day:

Running the following macro will allow you to select the picture that you
want to insert, then it inserts the picture and the name of the file after
the picture in a paragraph that is center aligned:

Dim txtPhotoPath as String
With Dialogs(wdDialogInsertPicture)
If .Show Then
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0)
End If
Selection.InsertAfter vbCr & txtPhotoPath
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

You could modify it so that it also inserted a Figure number.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <Da...@discussions.microsoft.com> wrote in message

news:B86493C0-452E-4BE3...@microsoft.com...

davidwilliams

unread,
Oct 11, 2009, 3:22:19 PM10/11/09
to
Thanks Doug, that worked really well for me.

How would I modify it so that it worked whilst inserting say 20 pictures at a time. When I try this code it only puts one caption at the bottom.

Your help is appreciated, thanks

Dave

Doug Robbins - Word MVP wrote:

Re: AutoCaption With the Filename of the Image
09-Jul-08

I posted this for someone else the other day:

Running the following macro will allow you to select the picture that you
want to insert, then it inserts the picture and the name of the file after
the picture in a paragraph that is center aligned:

Dim txtPhotoPath as String
With Dialogs(wdDialogInsertPicture)
If .Show Then
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0)
End If
Selection.InsertAfter vbCr & txtPhotoPath
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

You could modify it so that it also inserted a Figure number.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <Da...@discussions.microsoft.com> wrote in message
news:B86493C0-452E-4BE3...@microsoft.com...

EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 3.0 Cookbook [O'Reilly]
http://www.eggheadcafe.com/tutorials/aspnet/59386eb3-3049-46a7-8f07-63fe760096a7/book-review-c-30-cookb.aspx

davidwilliams

unread,
Oct 11, 2009, 3:25:53 PM10/11/09
to
Hi Doug
Thanks for that - works a treat.
How would I modify it to apply to deal with, say, 20 pictures at the same time?

You help is appreciated - thanks

Dave

Doug Robbins - Word MVP wrote:

Re: AutoCaption With the Filename of the Image
09-Jul-08

I posted this for someone else the other day:

Running the following macro will allow you to select the picture that you
want to insert, then it inserts the picture and the name of the file after
the picture in a paragraph that is center aligned:

Dim txtPhotoPath as String
With Dialogs(wdDialogInsertPicture)
If .Show Then
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0)
End If
Selection.InsertAfter vbCr & txtPhotoPath
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

You could modify it so that it also inserted a Figure number.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <Da...@discussions.microsoft.com> wrote in message
news:B86493C0-452E-4BE3...@microsoft.com...

EggHeadCafe - Software Developer Portal of Choice
Delegates to the Event
http://www.eggheadcafe.com/tutorials/aspnet/0fa76661-00cb-4777-810a-6cb6fed2ca82/delegates-to-the-event.aspx

Doug Robbins - Word MVP

unread,
Oct 12, 2009, 5:49:39 AM10/12/09
to
The following code will display a dialog in which you can select the folder
that contains the picture files and then it will insert each of the pictures
into a document with the filename of each following the picture

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim myrange As Range
'Select the folder that contains the files
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
'Close all open documents before beginning
Documents.Close SaveChanges:=wdPromptToSaveChanges
'Get the new title to be used for each document
Documents.Add
myFile = Dir(PathToUse & "*.*")
While myFile <> ""
With ActiveDocument
Set myrange = .Range
myrange.Collapse wdCollapseEnd
.InlineShapes.AddPicture PathToUse & myFile, , , myrange
Set myrange = .Range
myrange.Collapse wdCollapseEnd
myrange.InsertAfter vbCr & myFile & vbCr
End With
myFile = Dir()
Wend


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"David Williams" wrote in message news:2009101115...@gmail.com...

Chad Sowald

unread,
Oct 13, 2009, 2:22:30 PM10/13/09
to
On Oct 12, 5:49 am, "Doug Robbins - Word MVP"
> "David Williams" wrote in messagenews:2009101115...@gmail.com...
> >http://www.eggheadcafe.com/tutorials/aspnet/59386eb3-3049-46a7-8f07-6...

Hi Doug. I get an error on the line: myFile = Dir(PathToUse &
"*.*") with an error message of "Bad file name or number".

Do you know what would be wrong? I haven't modified your code at all.

Chad Sowald

unread,
Oct 13, 2009, 2:25:13 PM10/13/09
to

Hey Doug. Nevermind, turns out the file path was too long. I moved
the images to another directory (closer to the root) and it worked
great. Thanks!

DP

unread,
Nov 30, 2009, 7:39:01 PM11/30/09
to

Is there a way to control the order in which the images will be inserted?
(i.e. sorted by filename, date modified, or something else)

Doug Robbins - Word MVP

unread,
Nov 30, 2009, 10:32:07 PM11/30/09
to

Use the information in the article
http://word.mvps.org/faqs/MacrosVBA/InsertFileNames.htm to create a list of
the files, sort them into the order that you want and then use that file as
the data source for mailmerge to insert the pictures into a document.

See the "merge in a picture from a database" item on the following page of
fellow MVP Cindy Meister's website:

http://homepage.swissonline.ch/cindymeister/mergfaq1.htm

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.

"DP" <D...@discussions.microsoft.com> wrote in message
news:37F12A7F-1E0D-4FD7...@microsoft.com...

minhnguy...@gmail.com

unread,
Oct 11, 2016, 12:13:30 AM10/11/16
to
Thank you so much. You saved my day :x
0 new messages