Word and Macros

L

lizzzard59

Hi,

I'm relatively new to macros. I'm trying to create a macro that will
allow me to insert multiple photos into a new word document. By using
recorder, I was able to create something close to what I want, but it
uses the addpicture function(?) and only allows you to insert one
certain photo....I want to be able to add anywhere from 4 to 25 photos
at a shot, then be able to tab down to each caption (see below) and
fill in the blanks....

Sub test()
'
' test Macro
' Macro recorded 07/15/06 by Kevin Smith
'
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.InlineShapes.AddPicture FileName:= _
"C:\Documents and Settings\Owner\My Documents\My
Pictures\2004_02_15\100_0030.JPG" _
, LinkToFile:=False, SaveWithDocument:=True
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText Text:="Claim #:"
Selection.TypeParagraph
Selection.TypeBackspace
Selection.TypeText Text:=" "
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.TypeText Text:=Chr(11) & "Date taken:" & vbTab
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.TypeText Text:=" By: "
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.TypeText Text:=" Description: "
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.TypeParagraph
End Sub

Also on my wish list is the ability to change the size of each photo as
they are inserted so I don't have to do it manually. Any help you can
give would be greatly appreciated.
 
J

Jezebel

First: rewrite your code as a function that takes a filename as an argument.
Use that argument where currently you have the literal filename. While
you're at it, remove all the macro-recorder crap from the code, and replace
the use of the Selection object with Range objects.

Second: decide how you're going to get the list of file names and the
associated text. You could use a common dialog control on a UserForm (you
don't need to display the form to use the dialog) to get the list of
filenames, and insert them with placeholders for the other information to
entered directly into the document; or set up a table, in the document or on
a UserForm, to enter all the information up front.

Third: set up a function that gets the list of file names, then iterates the
list calling your function for each one.
 
L

lizzzard59

hmmmm.....
First: rewrite your code as a function that takes a filename as an argument.
Use that argument where currently you have the literal filename. While
you're at it, remove all the macro-recorder crap from the code, and replace
the use of the Selection object with Range objects.

Second: decide how you're going to get the list of file names and the
associated text. You could use a common dialog control on a UserForm (you
don't need to display the form to use the dialog) to get the list of
filenames, and insert them with placeholders for the other information to
entered directly into the document; or set up a table, in the document or on
a UserForm, to enter all the information up front.

Third: set up a function that gets the list of file names, then iterates the
list calling your function for each one.

Just underwent total brain melt down ! ! :)

First: I appreciate your input
Second: I have no clue how to do the things you suggested...
Third: See second above

I was hoping it would be something as simple as adding something in
place of the file address shown after AddPicture to something that
would bring up a folder search type option, followed by something that
would place them in order with the photo description, etc beneath each
one. Guess nothing is simple huh? To be honest, I'd rather learn how
to do something like you suggested on my own (and will do so down the
road), but for now I'm too busy at work to even think about popping
open a Learning VB book. Hence, my request for a quick fix......btw,
are there any books out there that you would highly suggest to get me
on the road to writing decent macros?
 
L

lizzzard59

Thanks Doug, but I don't think that's really what I need as the images
change each time I need to do this. Maybe I need to provide a little
more information.

I'm an insurance adjuster. When I look at cars, or take photos of an
accident scene, I need to provide my clients with what we in the claims
industry call a "photo log". For our current photo log, we use Word.
If Excel would work better for this, then I'm willing to try it also.
Basically our current photo log is a word file with Headers and Footers
that identify our company. In the body of the photo log, we've
inserted mutliple copies of a string of words identifying the
photo...i.e... photo #, date taken, by:, and description. Right now we
basically insert a photo right above the description area, right click
it and resize it to fit (4x6), then fill in the information below it
that describes the photo, who took it, etc. Then we start all over
again with the next photo. With only 5 or 6 photos it's not so bad.
When we have to "mount" 25 of them, it's very time consuming. With
each photo log we create, we use different photos that are taken out of
each "claim folder", so the photos we use are not static. Each photo
log is unique.

We've experimented with a program called Ifran in which we selected
multiple photos as "thumbs",exported them into a formated html
document, and then printed them as a PDF. This is quick and easy and
"most" of the time it worked, however on some occasions it would not
format correctly and instead of having two photos on one page with the
descriptions below them, we'd have 3 photos on one page, the
description of the 3rd photo would be on the next page and the
formatting would be all screwed up. So, I decided to look back at word
again and see if there was an easier way to do it using macros.

I was able to create a "recorded macro" that worked for inserting one
"static" photo, (it used AddPicture), but can't figure out how to
record one that gives you the option of picking the photos from folder
x, y, or z. Hope all of the above makes sense. In summary, this is
what my ideal macro would do:

1. Allow me to chose multiple photos to insert into Word
2. Allow said photos to be placed directly above my description line
or automatically add a preformated caption with my description items
included (I would then fill in the photo number, date taken, etc)
3. Allow me to resize the photos to fit my "template" without having
to do it one by one (not as critical as #1 and 2)

Can any or all of the above be done using macros? Or should I be
looking elsewhere for something like this? Thanks again and I
appreciate your patience.

Kevin
 
D

Doug Robbins - Word MVP

It is certainly possible to write some code to do what you want. I would
not however rule out a catalog or directory type mailmerge as a way of doing
it.

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

Ed

Kevin:

I have a macro that might be able to be "smushed" into something that will
work for you. It takes all the photos in one folder and inserts them into a
document. I use IrfanView as well, and you should be able to create a
separate folder of all pics to be used in one report and move them there
with IrfanView. You can add code in the macro to size either the height or
the width of each photos, with the other dimension scaled as appropriate.
The macro also currently inserts the file name, but that can easily be
changed to a block of text you type in at the beginning. Tabbing through to
each caption is a bit beyond the scope of the macro (and my abilities!), but
a Find/FindNext wouldn't be too hard.

To create a list of files from multiple folders that can be simply iterated
through and inserted is a bit more difficult. I'm muddling my way through a
similar kind of project, but I'm using Visual Basic 6.0, rather than a Word
macro, as I don't know how to get the file tree in a UserForm in Word VBA.

--
Ed
Chief Chef,
Kludge Code Cafe
"Spaghetti Is Our Specialty!"
'
 
L

lizzzard59

I would be interested in trying that macro out. How do I get a copy or
can you post it here? Thanks in advance!

Kevin
 
E

Ed

Here's my macro, Kevin. I hope it works well for you. Please do not use it
on anything irreplaceable until you've determined it works well for you.
(That's a good general caution as well as a statement of my confindence in
my own code! <g> )

'********
Sub InsAllPics()

Dim strFldr As String
Dim strFName As String
Dim cntFiles As Long
Dim thisFile As Long
Dim cntShape As Long
Dim oIShape As InlineShape
Dim strLookIn As String
Dim strPName As String
Dim ChrPos As Integer

strLookIn = GetFolderName("Choose a folder")

cntShape = 0
thisFile = 1
With Application.FileSearch
.LookIn = strLookIn
.FileName = "*.jpg"
.Execute

cntFiles = .FoundFiles.Count

Do
strFName = .FoundFiles(thisFile)
Set oIShape = Selection.InlineShapes.AddPicture(strFName)

' The following bits were used to set the height and width
' of an inserted graphic or photo.
''' oIShape.LockAspectRatio = msoTrue
''' oIShape.Height = InchesToPoints(9)
''' oIShape.ScaleWidth = oIShape.ScaleHeight

''' If oIShape.Height > 360# Then
''' oIShape.Height = InchesToPoints(5)
''' oIShape.ScaleWidth = oIShape.ScaleHeight
''' End If

thisFile = thisFile + 1

Selection.Collapse Direction:=wdCollapseEnd
Selection.TypeParagraph

' This part is where the file name is inserted
' You can substitute another string
ChrPos = InStrRev(strFName, "\")
strPName = Right(strFName, Len(strFName) - ChrPos)
Selection.Text = strPName
Selection.Collapse wdCollapseEnd
''Selection.InsertBreak Type:=wdPageBreak
Selection.TypeParagraph
Selection.TypeParagraph

' This code has been through some adjustments, and I
' don't always remove the bits and pieces from before.
' As it is right now, it puts paragraphs after each inserted
' photo. As it was before, using the sizing options above,
' it inserted two photos next to each other and then inserted
' paragraphs using the bit below.
''' cntShape = cntShape + 1
''' If cntShape = 2 Then
''' Selection.TypeParagraph
''' cntShape = 0
''' End If

Loop Until thisFile > cntFiles

End With

End Sub

''' This function opens a folder list box to let you
''' select the folder containing your photos. Don't
''' forget to set the library reference.
Function GetFolderName(sCaption As String) As String
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem

On Error GoTo CleanUp

Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item

GetFolderName = Item.Path

CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing

End Function
'***********

--
Ed
Chief Chef,
Kludge Code Cafe
"Spaghetti Is Our Specialty!"
'
 
K

KVSmith59

Thanks Ed....sorry it took me so long to reply. As I noted at the beginning
of this thread, I really am clueless about macro writing. I tried your macro
in vba, and when I tried to run it, I got an error message that said "user
defined type not defined" in this section of the macro. The part that came
up highlighed is surrounded by >> <<< . I'm not sure how to set a library
reference. Do you know of a good book or website where I can teach myself
the basics? Thanks again! :

''' This function opens a folder list box to let you
''' select the folder containing your photos. Don't
''' forget to set the library reference.

'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem

On Error GoTo CleanUp

Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item

GetFolderName = Item.Path

CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing

End Function
 
J

Jonathan West

In the VBA editor, go to the Tools menu and select References.

On the dialog that appears, scroll down until you find the item 'Microsoft
Shell Controls And Automation', and check the box against that entry.

Now, references in the code to objects in that library will work.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

Ed

Kevin -
Thanks Ed....sorry it took me so long to reply. As I noted at the
beginning
of this thread, I really am clueless about macro writing.
Do you know of a good book or website where I can teach myself
the basics?

I'm glad to see Jonathan has answered the References question by being as
clear as I should have been at first! Sorry about that.

As far as earning Word VBA, I'd recommend exactly what I did when I started
a little over two years ago:
(1) Check out http://word.mvps.org/FAQs/MacrosVBA/index.htm.
(2) Record a macro and look at the code. Try to figure it out. (In Word,
you can access you list of macros by hitting ALT+F8 or under
Tools>>Macro>>Macros. Select one to highlight it and click "Edit" - Word
will open the Visual Basic Editor (VBE) and show you the code for the macro.
You can set your cursor in almost any term and hit F1 to bring up the Help
file on that term.)
(3) Hang out in this newsgroup and read. Learn how to Google the groups -
you'll find most of your questions have already been asked and answered.
That's why we like doing things here on the NG rather than by email - the
information stays here for others to learn from. (I like to use
http://www.rondebruin.nl/Google.htm)
(4) Come up with a task for a macro (easy ones first!) and try to work
through it. Post here and ask questions about making it better.

Oh - and try out everything on non-critical files first! (Don't ask me how
I learned that!) 8>\

Have fun!
Ed
 

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