sharens1
06-30-2017, 05:51 PM
Hello, I am a noobie, and am looking for some sympathy from a VBA coder. I am trying to create a Macro, which will insert 4 large images per page in two columns and two rows. I would like the picture file name at the top, and a figure field (numbered) at bottom. I would like all pictures centered in the page. Any help is greatly appreciated. This is what I have liberated thus far (I cant take any credit for this). The code does not do this yet:
centers pictures in table
Inserts figure reference (Figure 1, etc. numerically)
Would like a maximum of four images, which would take a majority of the page. The images would be likely 40%-50% current size.
Any help is greatly appreciated.
Sub AddPics()
Application.ScreenUpdating = False
Dim oTbl As Table, i As Long, j As Long, k As Long, StrTxt As String
'Select and insert the Pics
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
'Add a 2-row by 2-column table with 7cm columns to take the images
Set oTbl = Selection.Tables.Add(Selection.Range, 2, 2)
With oTbl
.AutoFitBehavior (wdAutoFitFixed)
.Columns.Width = CentimetersToPoints(7)
'Format the rows
Call FormatRows(oTbl, 1)
End With
For i = 1 To .SelectedItems.Count
j = Int((i + 1) / 2) * 2 - 1
k = (i - 1) Mod 2 + 1
'Add extra rows as needed
If j > oTbl.Rows.Count Then
oTbl.Rows.Add
oTbl.Rows.Add
Call FormatRows(oTbl, j)
End If
'Insert the Picture
ActiveDocument.InlineShapes.AddPicture _
FileName:=.SelectedItems(i), LinkToFile:=False, _
SaveWithDocument:=True, Range:=oTbl.Rows(j + 1).Cells(k).Range
'Get the Image name for the Caption
StrTxt = Split(.SelectedItems(i), "\")(UBound(Split(.SelectedItems(i), "\")))
'Insert the Caption on the row above the picture
oTbl.Rows(j).Cells(k).Range.Text = Split(StrTxt, ".")(0)
Next
Else
End If
End With
Application.ScreenUpdating = True
End Sub
Sub FormatRows(oTbl As Table, x As Long)
With oTbl
With .Rows(x + 1)
.Height = CentimetersToPoints(7)
.HeightRule = wdRowHeightExactly
.Range.Style = "Normal"
End With
With .Rows(x)
.Height = CentimetersToPoints(0.75)
.HeightRule = wdRowHeightExactly
.Range.Style = "Normal"
End With
End With
End Sub
centers pictures in table
Inserts figure reference (Figure 1, etc. numerically)
Would like a maximum of four images, which would take a majority of the page. The images would be likely 40%-50% current size.
Any help is greatly appreciated.
Sub AddPics()
Application.ScreenUpdating = False
Dim oTbl As Table, i As Long, j As Long, k As Long, StrTxt As String
'Select and insert the Pics
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
'Add a 2-row by 2-column table with 7cm columns to take the images
Set oTbl = Selection.Tables.Add(Selection.Range, 2, 2)
With oTbl
.AutoFitBehavior (wdAutoFitFixed)
.Columns.Width = CentimetersToPoints(7)
'Format the rows
Call FormatRows(oTbl, 1)
End With
For i = 1 To .SelectedItems.Count
j = Int((i + 1) / 2) * 2 - 1
k = (i - 1) Mod 2 + 1
'Add extra rows as needed
If j > oTbl.Rows.Count Then
oTbl.Rows.Add
oTbl.Rows.Add
Call FormatRows(oTbl, j)
End If
'Insert the Picture
ActiveDocument.InlineShapes.AddPicture _
FileName:=.SelectedItems(i), LinkToFile:=False, _
SaveWithDocument:=True, Range:=oTbl.Rows(j + 1).Cells(k).Range
'Get the Image name for the Caption
StrTxt = Split(.SelectedItems(i), "\")(UBound(Split(.SelectedItems(i), "\")))
'Insert the Caption on the row above the picture
oTbl.Rows(j).Cells(k).Range.Text = Split(StrTxt, ".")(0)
Next
Else
End If
End With
Application.ScreenUpdating = True
End Sub
Sub FormatRows(oTbl As Table, x As Long)
With oTbl
With .Rows(x + 1)
.Height = CentimetersToPoints(7)
.HeightRule = wdRowHeightExactly
.Range.Style = "Normal"
End With
With .Rows(x)
.Height = CentimetersToPoints(0.75)
.HeightRule = wdRowHeightExactly
.Range.Style = "Normal"
End With
End With
End Sub