Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hard one. insert pictures in word from file list 2

Status
Not open for further replies.

kaloger

Technical User
Dec 1, 2004
10
US
i have an excel spread sheet that i use to store names, addresses, phone numbers, email addresses, etc. i have been importing it into word and then doing the tables to text thing to get it into a nice format for printing. i moved it to word so i can use the book fold print feature that word has. that was all fun and easy and worked well.

but now i want to add photographs. so i have a file name with each record for the photo. but i haven't a clue as to how to get word to grab the file name, delete it (i don't need the name in the printout), use the file name to open the picture and place it next to the data (family names, phone, email etc).

i am pretty sure something like this can be done. i can place the picture manually. but i have 300 pictures to bring in. seems like something code ought to do.

thanks
bill

=============================================
Start with:
---------------------

John doe
123 west street
Smallville, wa, 98258
Phone: 123 425 555-1234
Email: xyzzy@whatever.com
Dsc_1234.jgp

Jane smith
321 east street
Smallville, wa, 98258
Phone: 123 425 555-4321
Email: noway@whatever.com
Dsc_1254.jgp

Finish with:
---------------------------------

John doe <inserted picture>
123 street west
Smallville, wa, 98258
Phone: 123 425 555-1234
Email: xyzzy@whatever.com

Jane smith <inserted picture>
321 street east
Smallville, wa, 98258
Phone: 123 425 555-4321
Email: noway@whatever.com

 
r photos all in same folder?
have u tried the macro recorder to test doing an insert photo?

this is very possible, and not too difficult.
 
oh, and actually this not too hard at all, but other details needed.

1. are all photos same size?
2. do u need to resize them from original?
 
hi,

yes all photos are the same size. they are thumbnails so they are not too big. and yes they are all in the same directory. i tried recording my manual actions but could get that code to work. nor could i figure out how to get the file name from the document and replace in column next to text with the actual picture. word has me puzzeled. excel is easier to work with. i got it running in excel but never figured out how to get it into word.

thanks
bill
 
Not knowing the design exactly, hard to fully explain, but the syntax for inserting an image is:

Selection.InlineShapes.AddPicture FileName:= _
"C:\My Pictures\DSCN0030.JPG", LinkToFile:=False, SaveWithDocument _
:=True

If you insert text from the database actually inserts the filename, you would have to loop through the inserted text, extract the filename, concatenate it to the foldername holding the images, and use that as the full path/filename to insert the image. If the inserted text is in a table, then you would move the selection to the next cell before do that. deleteing the filename from the inserted text.

if you post more details can be more specific.

Gerry
 
hi Gerry

thanks for the tip. i will play with this. the insert text is in a table. i cut and paste all the data over from excel. what i need to know now is how to filename in the inserted text and move to the next column to insert the picture. and then go to the next group of text. each group of text is a paragraph. each line is forced with a newline character. is this the infor you asked for?

thanks
bill
 
Code:
Sub InsertPix()

Dim var
Dim strFilename As String
Dim counter As Integer
' go to top of doc
' then go to first table
    Selection.HomeKey unit:=wdStory
    Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
For var = 1 To Selection.Tables(1).Rows.Count
 ' selection goes to top of first cell
 ' if first cell does NOT have specific (5) paragraphs
 ' this needs to be adjusted
    If counter = 0 Then
        ' one time instruction on first cell
        Selection.MoveDown unit:=wdLine, Count:=5
        Selection.EndKey unit:=wdLine
        Selection.HomeKey unit:=wdLine, Extend:=wdExtend
    Else
' all other cells have full cell selected
' and therefore must be collapsed
' to get just the last sentence, minus paragraph mark
        Selection.Collapse direction:=wdCollapseEnd
        Selection.HomeKey unit:=wdLine, Extend:=wdExtend
    End If
' make string of the path and file name
' then delete selection text and backspace to remove
' the empty paragraph
    strFilename = "C:\Temp\" & Selection.Text
    Selection.Delete
    Selection.TypeBackspace
'move into next cell and add image
    Selection.MoveRight unit:=wdCell
    Selection.InlineShapes.AddPicture FileName:= _
        strFilename, LinkToFile:=False, SaveWithDocument _
        :=True
' increment counter and
' check if it is < number of rows
' if it is, go to next row, otherwise exit
    counter = counter + 1
    If counter < Selection.Tables(1).Rows.Count Then
        Selection.MoveRight unit:=wdCell
    Else
        Exit Sub
    End If
Next
End Sub

Still trying to make this use Range rather than Selection, but:

1. can not insert image with Range (seem to need Selection to insert an InLine object)

2. Word considers the contents of a cell, regardless of the number of paragraphs within it, to be ONE paragraph if cell contents used as a Range object.

Odd that.

Gerry
 
Gerry

Your code is very interesting. I am still studying it but am going slowly. I have a bad cold. What fun. But nevertheless i am still working it. I love figuring out code but my head is cloudy. I can see however that i didn’t really describe the problem well enough. I apologize.

The model gave was missing a key element. Each record is of varing length. I figured the solution would have a cleaver way of finding the last line in each record. Then the last record would need to be tested to see if indeed it was picture text. Some records are without pictures. Some records have

Name
Street Address
city
List of kid’s names
Phone
Cell phone
2nd cell phone
Email
2nd email
Picture file name (always last record)


An entry could have as few as 2 lines or as many as all the lines.

Some thoughts i had were i could put a blank record at the top to get it started. (seems there was an initiation issue that caused you to do something special for the first record) also i could put all the records as paragraphs and not in cells to start. But cells seem to make most sense as it gives a place to put the picture.

One other issue that i have struggled with is that if the record only has 3 lines in it, name, phone, picture, then the cell must be expanded with blank lines to allow the picture to fit. Or will putting the picture in the cell resize the cell to handle it?

Thanks
bill
 
I'm sorry, I asked for details, and if they are missing, then...well, there you go. I actually did make a point in the comments of the code that if the first cell does NOT have 5 lines it will have to be adjusted. Absolutely, you can indeed grab the last paragraph, and test if the file actually exists.

As for the last question. You stated the cell to contain the image was the NEXT column. The original cell will not expand with the image, but it will expand to match the NEXT cell with DOES have the image. That is, depending on how you have the table configured (are you using a Table Style?....betcha you are not).

A very important point which you did not state is you are now implying that each "line"
Name
Street Address
city
List of kid’s names

is in a separate cell? Not a paragraph in a cell. What on earth for? You say "i could put all the records as paragraphs and not in cells to start. But cells seem to make most sense as it gives a place to put the picture"

As in:

Name image
Street Address same image
city same image
etc.

Please note your use of plural. Cells Huh?

In any case, I am sure you will be able to adjust the code for your needs.


Gerry
 
hi gerry,

i think i just confused you. sorry. my head was a bit cloudy. no i am not using table styles. never heard of that. will look at it.

can you tell me how to test for existance of file before trying to open it? i have the rest figured. your code was very helpful for showing me the direction to go it. thanks.

bill
 
how to test for existance of file
If Dir(strFullPathname) = "" Then
MsgBox "Can't open " & strFullPathname
End If-

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top