insert multiple images into doc

J

jgershonw1

I want to insert 3(rows)x2(cols) images per page into a document, but I
am having problems inserting the pagebreaks. I can't seem to get the
page break to occur after images. Right now, the images are pushed
down a page, thus mixing up the order and the first page ends up blank.
If anybody can help me with solving the problem, I would be very
grateful. Thanks!

Here is the following function that will take a directory of jpgs
inserts them into a document:

Public Sub InsertGraphicFiles(ByVal strDir As String)
Dim doc As Document
Dim ShapeGraph As Shape
Dim strFile As String
Dim arrFiles(10000) As String
Dim lngCount As Long
Dim lngWidth As Long
Dim lngHeight As Long
Dim lngLeftOffset As Long
Dim lngTopOffset As Long
Dim i As Long

Dim rngEnd As Word.Range
Set rngEnd = ActiveDocument.Range

lngWidth = 200
lngHeight = 170

Set doc = ActiveDocument

Dim ilshape As InlineShape
For Each ilshape In doc.InlineShapes
ilshape.Delete
Next

strFile = Dir(strDir & "\*.jpg")
i = 0
Do While strFile <> ""
arrFiles(i) = strDir & "\" & strFile
strFile = Dir
i = i + 1
Loop
lngCount = i - 1

lngLeftOffset = 20
lngTopOffset = 20

Dim lngMaxColsPerPage As Long
Dim lngMaxRowsPerPage As Long
Dim lngColCount As Long
Dim lngRowCount As Long
Dim lngLeft As Long
Dim lngTop As Long

lngMaxColsPerPage = 2
lngMaxRowsPerPage = 3

lngColCount = 0
lngRowCount = 0
lngLeft = lngLeftOffset
lngTop = lngTopOffset
For i = 0 To lngCount
Set ShapeGraph = doc.Shapes.AddPicture(FileName:=arrFiles(i),
LinkToFile:=False, SaveWithDocument:=True, Anchor:=Selection.Range)
ShapeGraph.Width = lngWidth
ShapeGraph.Height = lngHeight

If lngColCount > 0 Then
lngLeft = lngLeft + ShapeGraph.Width + 20
Else
lngLeft = lngLeftOffset
End If

ShapeGraph.Top = lngTop
ShapeGraph.Left = lngLeft
ShapeGraph.WrapFormat.Type = wdWrapThrough

lngColCount = lngColCount + 1
If lngColCount > lngMaxColsPerPage - 1 Then
lngTop = lngTop + ShapeGraph.Height + 20
lngLeft = lngLeftOffset
lngColCount = 0
lngRowCount = lngRowCount + 1
End If

If lngRowCount > lngMaxRowsPerPage - 1 Then
Selection.InsertBreak Type:=wdPageBreak
lngTop = lngTopOffset
lngRowCount = 0
End If
Next

End Sub
 
D

Doug Robbins - Word MVP

Use the following code to create a list of the filenames of the images:

Sub InsertNamesOfFilesInAFolder()

Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop

'collapse the selection
Selection.Collapse wdCollapseEnd


End Sub

The in the document, create a 3 row by 2 column table with the appropriate
cell dimensions (or use a label template) and then use mailmerge to merge
the images into the document.

See the "Graphics from data base" item under the "Special merges" section of
fellow MVP Cindy Meister's website at

http://homepage.swissonline.ch/cindymeister/MergFram.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

jgershonw1

Thanks for the response. I have most of the code elements as shown in
my original post. The trick is to get the page breaks where I want
them. I forgot to add that it will work if I manually intervene by
placing a break point on the line "Selection.InsertBreak
Type:=wdPageBreak". When the code breaks at that point, I will
manually place a space at the current cursor position in the document
and then continue executing the code. The page break will now come
after the images and not push them to the next page. I continue doing
this until all of the images are inserted. What is that I am missing
that is causing this behaviour with the page breaks?
 
N

Neale

I don't know for sure, but is there some way to see the anchor points in your
code for the inserted pictures to ensure that your page break occurs after
them?
 

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