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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Insert images into table 3

Status
Not open for further replies.

happyaslarryTT

Technical User
Jan 28, 2009
2
Dear Experts

I export a series of paths from an Access database which specified plumbing fittings and would like to automate the insertion of images in a word document for each fitting.

The paths will be put into a table in the word document as shown below. The table could have a different number of rows or columns each time.

F:\Images\Vaillant\Boiler.jpg F:\Images\Ideal\Basin.jpg
F:\Images\Bristan\Big Tap.jpg F:\Matki\Shower.tif

I want to program VBA to read the path from each cell in the table and insert the corresponding image from the path.

I know a little Access VBA but I have no idea how to do this using the Word VBA language. Any help would be greatly apprecciated.

Many thanks,

Laurence
 
So you have a document with some number of tables. All the tables in the document belong to the Tables collection. You need to know which table has the references (file names) you want. Let's say it's "1".

Set a reference to the table:
Code:
set tb=thisdocument.tables(1)

Now each cell the table can be index-referenced but as far as I know, there is no Cells collection for tables. There are, however Rows and Columns so what I've done in the past is to loop over rows and columns referencing the cells:
Code:
rws=tb.rows.count
cls=tb.columns.count
for r = 1 to rws
  for c = 1 to cls
    fn=tb.cell(r,c).range.text 'get file name from cell
    ' move to where you want to insert the picture
    selection.InlineShapes.AddPicture filename:=fn
  next
next

One other thing to note, I seem to get 2 characters on the end due to something the table is doing so you might need to trim them off:
Code:
fn=left(fn,len(fn)-2)

_________________
Bob Rashkin
 
Bong: "there is no Cells collection for tables."

No, not directly, but there IS table.Range.Cells, which is a Collection.

True, the .Range.Text of a table cell must be adjusted. I use a Function as I need this all the time. So....
Code:
Option Explicit

Function CellText2(aCell As Cell) As String
    Dim sText As String
    sText = aCell.Range.Text
    CellText2 = Left(sText, Len(sText) - 2)
End Function

Sub yadda()
Dim oTable As Table
Dim oCell As Cell
Dim strCell As String
Set oTable = ActiveDocument.Tables(1)
   For Each oCell In oTable.Range.Cells
      strCell = CellText2(oCell)
      If Right(strCell, 3) = "jpg" Or _
         Right(strCell, 3) = "tif" Then
         With oCell.Range
            .Delete
            .InlineShapes.AddPicture _
               FileName:=strCell, _
               LinkToFile:=False, _
               SaveWithDocument:=True
         End With
      End If
   Next
End Sub
1. declare a table object, and a cell object, and a string variable

2. set the table object

3. for each cell in table, make the string variable the text content of that cell - using the Function to strip off the end-of-cell marker to get just the text

4. if the last three characters are either "jpg" or "tif" then

5. empty the cell range (the F:\Images\Vaillant\Boiler.jpg) using .Delete

6. add the image using the string variable as the filename

By using objects you avoid the use of counters (rows and columns).

By using the Range of the cell you avoid the use of Selection.

Gerry
 
cool (as always)

_________________
Bob Rashkin
 
Excellent.

It's all working now and should save us several days of donkey work over the coming year. My colleague is over the moon that she will no longer need to dig through folders.

Thank you very much for your help guys.

Best wishes,

Laurence

 



Laurence,

I notice that you are a brand new member of Tek-Tips. You might want to consider thanking at lease one, if not both of the contributors to this thread by...
[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].

The little purple Stars accomplish several important things.

First, it gives positive feedback to contributors, that their posts have been helpful.

Second, it identifies threads as containing helpful posts, so that other members can benefit.

And third, it identifies the original poster (that's YOU, BTW), as a grateful member, that not only receives, but is willing to give tokens of thanks.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top