PDA

View Full Version : Solved: How to insert pictures in word table from list of images?



TechNet
08-19-2005, 07:59 AM
Hi,

I am new to word VBA and trying automate this process. I have to insert the pictures in fixed cells from a directory where the images are available with the names 1.jpg, 2.jpg etc...
Please help me in this situation.

Thanks in advance
TechNet

fumei
08-19-2005, 08:47 AM
First of all, we ARE talking about Word, correct?

Second, we are talking about cells in a Word table, correct?

Third, we are talking about FIXED cell dimensions, correct?

Fourth, say you have a folder of images...OK, is there a required order for these to go into the table cells? Left to right, by filename? Top to bottom, by file size? By rows, each row by image size? It does not matter? Maybe the smaller image sizes at top? Are all the image sizes the same? Are you going to need to resize them?

As you can see, your question has not give enough detail.

Can you fill a table with images? Of course you can. Can you fill it efficiently, automatically, by criteria that you have not stated? Maybe...state your criteria.

TechNet
08-19-2005, 11:11 AM
Gerry,

I am sorry for not giving clear details, but you guessed exactly what I need. I have a table with fixed cells in word document (2003 version). I have some images (jpg files) with different sizes and numeric numbering for names. They need to go to each row of the first column with the respective images. Example:1.jpg should be inserted in to 1st row, 2.jpg should be in 2nd row. I might need to resize the picture according to the size of the cell.
I really appreciate, if you can show me the path, please let me know if you need any more details.
Thanks a lot
TechNet (seems like not suitable for me!)

fumei
08-19-2005, 11:18 AM
OK, the main question is:

Why do you need to resize the image...rather than resize the cell? WHY is the cell fixed?

fumei
08-19-2005, 11:21 AM
Oh, and...is this a ONE column table? You do not mention other columns.

Is it Ok for the table to span pages?

Does the table have a header row?
Does the cells also contain information on the file?

As you can see, what makes things work is good analysis and design. And THAT only comes from good information.

Better specs, bettter code.

Also....have you tried using the macro recorder yet? If not....do so.

fumei
08-19-2005, 11:23 AM
Oh....and is this table of known dimensions? Do you know how many rows there are, or are you creating this table as well. Is the table going to match the number of JPG files in that particular folder?

How are you getting the folder information?

TechNet
08-19-2005, 11:36 AM
Gerry,

You are so quick. First of all, the table has 2 columns. 1st column should be with image and the 2nd with some text like image name etc... I do not want the table to be spanned across pages, that is why I created as fixed (cell) table. Table has 2 columns and 5 rows. I have "C:Images" folder with 5 images named as 1.jpg, 2.jpg...
Is there any way to fit (resize) the image to the cell height and width with maintaining the aspect ratio? This particular cell is going to have only image, no text.

Looking forward to hear from you.
Thanks
TechNet

fumei
08-19-2005, 11:40 AM
Sub JPGIntoTable()

Dim mFile
' make a 1 row, 1 column table
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=1, NumColumns:=1, _
AutoFitBehavior:=wdAutoFitFixed

' set up DIR folder and parameter
mFile = Dir("c:\test\*.jpg")

' while DIR is NOT empty
Do While mFile <> ""
' insert the jpg file into the table
Selection.InlineShapes.AddPicture FileName:= _
"c:\test\" & mFile _
, LinkToFile:=False, SaveWithDocument:=True
' moves the Selection one cell, as there is no cell
' this is the same as Tab, and Word MAKES
' another row
Selection.MoveRight Unit:=wdCell
mFile = Dir
Loop
' delete that final empty row
Selection.Rows.Delete
End Sub

This may get you started. Dir is useful for simple stuff. This takes all .JPG files in the c:\test folder, and put them into a one column table. It makes the same number of rows as the number of jpg found in that folder.

This is very basic, and can be set up much better. However, until I have an idea of exactly what you are talking about....shrug.....

fumei
08-19-2005, 11:41 AM
Ooops. I posted before you responded. OK, let me see....
Hang on.

fumei
08-19-2005, 12:02 PM
Oh, and I do not understand the last sentence...WHAT particular cell????

"some text like image name etc... " Sorry, but I think I have been trying to make it clear that things like "etc" have to be used quite sparingly when you are talking about coding anything.

I have NO idea what etc means, and have NO idea if whatever it does mean is possible. Image name....well, FILE name is possible....

fumei
08-19-2005, 12:05 PM
OK - version #2. This uses FileSystemObject, rather than Dir.

You MUST, repeat MUST, have a reference to Microsoft Scripting Runtime. In the Visual Basic Editor go to Tools > References. If Microsoft Scripting Runtime is not near the top, and checked, scroll down until you find it, and check it. Select OK.

Sub JPGIntoTable()

Dim mFile
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File

Set fso = CreateObject("scripting.filesystemobject")
Set fld = fso.GetFolder("c:\test")

ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=5, NumColumns:=2, _
AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
With Selection.Tables(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.Spacing = 0
.AllowPageBreaks = True
.AllowAutoFit = False
End With
With Selection.Cells(1)
.WordWrap = True
.FitText = False
End With


For Each fil In fld.Files
If fil.Type = "JPEG Image" Then
With Selection
.InlineShapes.AddPicture FileName:= _
"c:\test\" & fil.ShortName, _
LinkToFile:=False, SaveWithDocument:=True
.MoveRight Unit:=wdCell
.TypeText Text:=fil.ShortName
.MoveRight Unit:=wdCell
End With
End If
Next fil

Set fld = Nothing
Set fso = Nothing
' get rid of that last row
Selection.Rows.Delete
End Sub

This has probably extraneous instructions for the table creation. Adjust to fit your table needs. I am still not quite getting what you want.

TechNet
08-19-2005, 02:21 PM
Thank you so much Gerry, you are so fast, I can't reach your code that fast. Let me use the code and see, hope this works.
Thanks again
TechNet

TechNet
08-29-2005, 07:33 AM
I got it, thank you so much Gerry.

fumei
08-29-2005, 09:56 AM
You are welcome.

Anne Troy
09-03-2005, 10:01 AM
I marked this solved. :)

TechNet: You can mark your threads solved using the Thread Tools dropdown at the top of your thread.

Hi, Gerry!!