Loading images into table cells

G

Griffin

First, I am having problems recording a skeleton macro to do some of this
because once the recording is strarted, I lose much of the cursor selection
functionality.

So, let me explain what I need.

I have a document that is populated with a large number of complex tables
(nested inside each other etc). But a large number of these cells contain
text that is a full path to image files. What I need to do is replace that
text with the actual image, resized to fit into the cell.

I had planned to record the code needed to:
- Find the next cell containing a fragment of the path
- Select the full text content
- Copy the text (file path)
- Insert the file using the copied info into the cell as a forground image,
replacing the existing text
- Repeat this until no more path fragments are found.

Can someone get me kick-started,

Thanks
 
J

Jay Freedman

This shouldn't be hard -- although the recorder isn't going to help very
much -- but first I need you to explain what you mean by a "fragment of the
path" and how that's related to the "full text content". Does one cell (no
matter how deeply nested) contain a complete path and filename, or is the
complete path scattered over several cells?

It might also be necessary to know whether the tables have any merged or
split cells (not just in the area that contains the paths, but anywhere in
the table).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Griffin

Jay,

Scattered through the document are whole cells that contain a relative path
with filename of the desired image. The path is relative to the location of
the document. Since all of these references share the same common beginning
of the path name, that can be used to scan for the next occurence. EG:

"site/images/products/full/S-SDH16.jpg"

and they all have the same path so a scan for
"site/images/products/" should find all of them.
 
J

Jay Freedman

Hi Martyn,

Good enough. Here's a working macro. As I said, there's no way you
could ever record anything like this.

Sub GetPictures()
Const PathFragment = "site/images/products/"
Dim oRg As Range
Dim oRgWork As Range

' set up the find (standard boilerplate)
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = PathFragment
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Do While oRg.Find.Execute
If oRg.Information(wdWithInTable) Then
' to force pictures to resize to cell,
' instead of vice versa
oRg.Tables(1).AutoFitBehavior wdAutoFitFixed

'set range to whole path
Set oRgWork = oRg.Cells(1).Range
With oRgWork
' exclude cell marker
.MoveEnd unit:=wdCharacter, Count:=-1

' insert picture
ActiveDocument.InlineShapes.AddPicture _
FileName:=oRgWork.Text, _
linktofile:=False, _
savewithdocument:=True, _
Range:=oRgWork

' remove text
oRgWork.MoveStart unit:=wdCharacter, Count:=1
oRgWork.Delete
End With
End If

' prepare for next .Execute
oRg.Collapse wdCollapseEnd
Loop
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Griffin

Bingo, that hit the nail on the head.
I did not expect to get a completely working routine, just the fundamentals!
That saved me a lot of head scratching.

The only thing I needed to change was to add the following two lines:

Dim sFileName as String
..
..
..
sFileName = Replace(.Text,"/","\") 'For a consistent filepath format

I have been using VB and its variants for some time now, but this is the
first use with Word. Somehow, the model has a very different "look and feel"
than its bretheren in Excel, Access, Outlook etc. and there are few good
references with usefull examples to be found.

Thanks!!
 

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