![]() |
|
#1
|
|||
|
|||
|
I've got a user determined (from a form) number of images to add to an existing docm file.
I need to add a page below the page the cursor is on and then add the images to that and if needed subsequent new pages. Each page should have 3 columns (a table) with one image in each and as many rows as are needed (4 rows per page). The images are to be a specific width & height. I've tried many different approaches and no matter what I do, Word puts the images where it feels they should go without regard to my code, often on the same page the cursor is on. Also, it adds another blank page for no reason that I can see. How can I make Word do what I need? Below is my last attempt. Code:
Public Sub insertSectionPics()
Dim nPicsPage1 As Integer, nPicsPage As Integer, tblPics As Table, nRows As Integer, nPages As Integer, oPic As Word.InlineShape
Dim tblCell As Cell
Dim tblRow As Row
Dim i As Integer, x As Integer, nIdx As Integer, nInch As Integer, nWidth As Integer, nHeight As Integer
Dim nRects As Long
Dim oRange As Word.Range, oRngCaption As Word.Range
nWidth = 172
nHeight = 129
nInch = 72 'Points
nIdx = -1
If Not IsVarArrayEmpty(arSectionPics) Then
If arSectionPics(0) <> "" And sSelPage <> "" Then
nPicsPage1 = CInt(sSelPage)
nPicsPage = nPicsPage1 + 1
nRows = Round(((UBound(arSectionPics) + 1) / 3) + 0.4, 0)
Selection.GoToNext wdGoToPage
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious
Selection.InsertNewPage
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious
Selection.Text = "Add Pics Table"
Selection.Find.Text = "Add Pics Table"
Set oRange = ActiveDocument.Content
oRange.Find.Execute FindText:="Add Pics Table", Forward:=True
Set tblPics = ActiveDocument.Tables.Add(Range:=oRange, NumRows:=nRows, NumColumns:=3)
tblPics.Columns.DistributeWidth
For i = 1 To tblPics.Rows.Count
Set tblRow = tblPics.Rows(i)
tblRow.Cells.VerticalAlignment = wdCellAlignVerticalTop
tblRow.Height = nHeight + 16
For x = 1 To 3
nIdx = nIdx + 1
If nIdx <= UBound(arSectionPics) Then
Set tblCell = tblRow.Cells(x)
tblCell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Set oPic = tblCell.Range.InlineShapes.AddPicture(FileName:=arSectionPics(nIdx), LinkToFile:=False, SaveWithDocument:=True)
oPic.Height = nHeight 'Points
oPic.Width = nWidth 'Points
Set oRngCaption = ActiveDocument.Range(tblCell.Range.End - 1, tblCell.Range.End)
oRngCaption.ParagraphFormat.Alignment = wdAlignParagraphCenter
oRngCaption.Font.Bold = False
oRngCaption.Font.Name = "Arial"
oRngCaption.Font.Size = 9
If arSectionPicCaptions(nIdx) <> "" Then
oRngCaption.Text = arSectionPicCaptions(nIdx) 'vbCrLf &
Else
oRngCaption.Text = " "
End If
End If
Next
Next
End If
End If
bTablesLoaded = False
fmSectionPics.Hide
Set fmSectionPics = Nothing
End Sub
|
|
#2
|
||||
|
||||
|
Without knowing what's in your array (which is not defined in your macro) you probably need something like
Code:
Public Sub insertSectionPics(arSectionPics As Variant)
Dim oTable As Table
Dim oRng As Range, oCell As Range
Dim i As Long, j As Long
Set oRng = ActiveDocument.Bookmarks("\page").Range
oRng.Collapse 0
oRng.InsertBreak wdSectionBreakNextPage
oRng.start = oRng.start - 1
oRng.Collapse 1
Set oTable = ActiveDocument.Tables.Add(oRng, 1, 3)
oTable.AutoFitBehavior (wdAutoFitFixed)
j = 1
For i = LBound(arSectionPics) To UBound(arSectionPics)
If i > 0 And i Mod 3 = 0 Then
oTable.Rows.Add
j = j + 1
End If
Set oCell = oTable.Cell(j, i Mod 3 + 1).Range
oCell.End = oCell.End - 1
ActiveDocument.InlineShapes.AddPicture _
FileName:=arSectionPics(i), _
LinkToFile:=False, _
Range:=oCell
Next i
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Thanks gmayor: that helped.
The array is global and it has the file paths as you suspected. I didn't mention that there is a potential caption below each pic. If you look at my code you can see where I add space for it below each pic and if the caption exists to print it within that space. I need to make sure all the pics are aligned, with or without a caption. Also, there is no padding between the pics. Can that be added? The caption should take care of it vertically but I still need padding horizontally. |
|
#4
|
||||
|
||||
|
I think I would be inclined to add the captions in a second row underneath the row containing the images. That would ensure the images are aligned, and if you want horizontal padding, you can increase the cell margins to give you the extra space.
It just means that you start the table with two rows rather than one and add two rows for each three images and adjust the calculations accordingly to ensure images and captions go in the appropriate rows.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#5
|
|||
|
|||
|
Looks like this will work.
Thanks gmayor. Your code is concise and I have no idea how you learned how to do this. I never realized there are built in 'Bookmarks' or that Break was a permanent element in the document. Thanks again for your help. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using mail merge to make place cards. One record with data over two pages | drew.last | Mail Merge | 2 | 03-05-2015 04:37 PM |
how force Word to scroll to full page view
|
browns87 | Word | 2 | 08-05-2012 03:25 PM |
| Printing a Selection in Word and keeping it in the same place on the page | punkrae | Word | 0 | 03-29-2012 10:49 AM |
| Force a page break | Emaleth9999 | Mail Merge | 1 | 02-09-2012 02:36 AM |
Place pages in alpha order.
|
Wskip49 | Word | 5 | 08-28-2011 07:54 PM |