Insert Images in Word Document

K

Ken

Hi,

I have a dumb question that I couldn't find in the book
and website. How can I insert 5 images that are all jpg
files in a word document using the vba code. thanks!

ken
 
J

Jay Freedman

Ken said:
Hi,

I have a dumb question that I couldn't find in the book
and website. How can I insert 5 images that are all jpg
files in a word document using the vba code. thanks!

ken

Hi, Ken,

Here's some simple code that just sticks the pictures in next to each other
at the current cursor position, using whatever sizes they happen to be. If
you need to resize, reposition, caption, or otherwise adjust each picture,
the code can get more complicated.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Sub FiveJPGs()
Dim JPGFileName(5) As String
Dim Counter As Integer

'JPGFileName(0) = ""
JPGFileName(1) = "c:\graphics\firework.jpg"
JPGFileName(2) = "c:\graphics\stones.jpg"
JPGFileName(3) = "c:\graphics\nightwatch.jpg"
JPGFileName(4) = "c:\graphics\horsehead.jpg"
JPGFileName(5) = "c:\graphics\f18cloud.jpg"

For Counter = 1 To 5
Selection.Collapse direction:=wdCollapseEnd
ActiveDocument.InlineShapes.AddPicture _
FileName:=JPGFileName(Counter), _
linktofile:=False, savewithdocument:=True, _
Range:=Selection.Range
' go from left side of picture to right side
Selection.Move unit:=wdCharacter, Count:=1
Next Counter
End Sub
 
K

ken

Hi Jay,

Thanks! I am inserting picture in a table so I don't need
to resize it. Is there different between inserting in a
plain document or a table.
Ken
 
J

Jay Freedman

Hi, Ken,

For inserting pictures in a table, one per cell, you do need to change
the way the macro navigates through the document. Try something like
this:

Sub FiveJPGs()
Dim JPGFileName(5) As String
Dim Counter As Integer
Dim Rg As Range

'JPGFileName(0) = ""
JPGFileName(1) = "c:\clips\aldie mansion.jpg"
JPGFileName(2) = "c:\clips\aldie mansion bw.jpg"
JPGFileName(3) = "c:\clips\aldie mansion blue.jpg"
JPGFileName(4) = "c:\clips\balloons.jpg"
JPGFileName(5) = "c:\clips\candle2.jpg"

On Error GoTo bye

Set Rg = ActiveDocument.Tables(1).Cell(1, 1).Range

For Counter = 1 To 5
ActiveDocument.InlineShapes.AddPicture _
FileName:=JPGFileName(Counter), _
linktofile:=False, savewithdocument:=True, _
Range:=Rg
Set Rg = Rg.Cells(1).Next.Range
Next Counter
bye:
End Sub
 
K

Ken

Hi Jay,
Thanks! I try the sub. It works when I insert the images
in the same row. When it goes to next row, it would show
up error message "Object Variable or With Block variable
not set". And, it happened in the line, Set Rg = Rg.Cells
(1).Next.Range. It looks like document won't move to next
row because I try to debug in different ways. As long as
in the first row, everything looks fine.

Thank you very much!

Ken
 
J

Jay Freedman

Hi, Ken,

Before you run the macro, the table has to have at least as many cells
as there will be pictures, because the Rg.Cells(1).Next.Range doesn't
add a new row the way tabbing would if you were doing it manually.

I did test the macro for five pictures in a table with two rows and
three columns, and it filled the first row and two cells in the second
row.

Also, in order to see an error message, you must have removed or
commented out the On Error statement. If that's there, the macro will
just stop when it gets to the end of the table.
 
K

Ken

Hi Jay,
Thanks! I have two columns and three rows. And, the only
problem would happened in the first row and the second
columns. It will show "Object Variable or with block
object not set". I don't know why it only happens in one
cell. I have some more questions about the code. What
does Cell(1,1) in ActiveDocument.Table(1).Cell(1,1).Range
mean? Is it the index of Cell? And, the other one is what
does Cells(1) in Rg.Cells(1).Next.Range mean?...If I can
figure these, maybe I can find out what's going wrong
with my program...thanks!

Ken
 
G

Guest

Hi Jay,

I figured it out. Thanks!
-----Original Message-----
Hi Jay,
Thanks! I have two columns and three rows. And, the only
problem would happened in the first row and the second
columns. It will show "Object Variable or with block
object not set". I don't know why it only happens in one
cell. I have some more questions about the code. What
does Cell(1,1) in ActiveDocument.Table(1).Cell (1,1).Range
mean? Is it the index of Cell? And, the other one is what
does Cells(1) in Rg.Cells(1).Next.Range mean?...If I can
figure these, maybe I can find out what's going wrong
with my program...thanks!

Ken least
as many cells it cells
in the second in
.
 
J

Jay Freedman

Hi, Ken,

For the benefit of any other readers who had the same questions,
here's a short course in using Word's object model for tables...

The expression ActiveDocument.Tables(1) returns a Table object that
represents the first table in the document. That Table object has a
method named Cell, which takes two arguments -- a row number and a
column number -- and returns a Cell object representing the cell at
the intersection of that row and column. In this case I'm asking for
the cell in row 1 and column 1, or the top left-hand corner of the
table. That Cell object has a Range, which is the result of the full
expression

Set Rg = ActiveDocument.Tables(1).Cell(1, 1).Range

Going on to the other expression, the Range object has a method Cells
(not the same as the Cell method of the Table object). It takes only
one argument, an index number. When I pass it the index 1, it returns
the first (in this case the only) Cell object inside the range. So if
Rg is currently pointing to the range of the top left cell of the
table, Rg.Cells(1) returns the Cell object representing that cell. Now
the Cell object has a Next method that returns the next cell in the
table -- wherever the cursor would go if you tabbed from "this" cell.
The exception is that it won't create a new row if "this" cell is the
last column of the last row; instead it just returns the value
Nothing. Finally, the Cell object returned by Next has a Range, which
I assign to Rg -- the result being that Rg moves over to the "next"
cell, whether in the same row or in the next one, unless I'm already
at the end of the table.

If you want to emulate the special function of the Tab key adding a
new row, you'd have to program it explicitly.
 

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