AutoCaption With the Filename of the Image

D

David

(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.
 
D

Doug Robbins - Word MVP

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
 
D

David Williams

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 yo
want to insert, then it inserts the picture and the name of the file afte
the picture in a paragraph that is center aligned

Dim txtPhotoPath as Strin
With Dialogs(wdDialogInsertPicture
If .Show The
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0
End I
Selection.InsertAfter vbCr & txtPhotoPat
Selection.ParagraphFormat.Alignment = wdAlignParagraphCente
End Wit

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 m
services on a paid consulting basis

Doug Robbins - Word MV


EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 3.0 Cookbook [O'Reilly]
http://www.eggheadcafe.com/tutorial...8f07-63fe760096a7/book-review-c-30-cookb.aspx
 
D

David Williams

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 yo
want to insert, then it inserts the picture and the name of the file afte
the picture in a paragraph that is center aligned

Dim txtPhotoPath as Strin
With Dialogs(wdDialogInsertPicture
If .Show The
txtPhotoPath = WordBasic.FilenameInfo$(.Name, 0
End I
Selection.InsertAfter vbCr & txtPhotoPat
Selection.ParagraphFormat.Alignment = wdAlignParagraphCente
End Wit

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 m
services on a paid consulting basis

Doug Robbins - Word MV


EggHeadCafe - Software Developer Portal of Choice
Delegates to the Event
http://www.eggheadcafe.com/tutorial...810a-6cb6fed2ca82/delegates-to-the-event.aspx
 
D

Doug Robbins - Word MVP

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
in message news:[email protected]...
 
C

Chad Sowald

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



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

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.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 3.0 Cookbook [O'Reilly]
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.
 
C

Chad Sowald

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
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
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 thatyou
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

EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 3.0 Cookbook [O'Reilly]
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.

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!
 
D

DP

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 said:
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
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


EggHeadCafe - Software Developer Portal of Choice
Book Review: C# 3.0 Cookbook [O'Reilly]
http://www.eggheadcafe.com/tutorial...8f07-63fe760096a7/book-review-c-30-cookb.aspx
 
D

Doug Robbins - Word MVP

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 said:
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 said:
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
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


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

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