I'm trying to put together a photo album macro for Word...select a folder and have each of the image files inserted into table (2x1) with the image in the first row and the file name in the second row.
I basically have the general process for the first loop, but after that, instead of inserting a completely new table, everything I've tried either overwrites the first table, nests the new rows inside the first table, or adds new rows (with bad formatting) the the first table.
I'm obviously not exiting out of the first table properly, but can't figure out how...any suggestions?
Thanks!
I basically have the general process for the first loop, but after that, instead of inserting a completely new table, everything I've tried either overwrites the first table, nests the new rows inside the first table, or adds new rows (with bad formatting) the the first table.
I'm obviously not exiting out of the first table properly, but can't figure out how...any suggestions?
Thanks!
Code:
Format Page
Select Folder
For each image in folderInsert New (2x1) Table
Insert image
Insert caption
Exit out of TableNext image
Exit sub
Code:
Option Explicit
Dim oDoc As Word.Document
Dim oSec As Word.Section
Dim rng As Word.Range
Dim xFileDialog As FileDialog
Dim xPath, xFile As Variant
Dim str As String
Dim i As Integer, j As Integer, k As Integer, intTables As Integer
Sub PictureAlbum()
'
' https://www.extendoffice.com/documents/word/5451-insert-picture-with-filename-in-word.html
'
Set oDoc = ActiveDocument
Call SetPage
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
intTables = 0 ' set initial value
If xFileDialog.Show = -1 Then
xPath = xFileDialog.SelectedItems.Item(1)
If xPath <> "" Then
xFile = Dir(xPath & "\*.*")
Do While xFile <> ""
If UCase(Right(xFile, 3)) = "PNG" Or _
UCase(Right(xFile, 3)) = "TIF" Or _
UCase(Right(xFile, 3)) = "JPG" Or _
UCase(Right(xFile, 3)) = "GIF" Or _
UCase(Right(xFile, 3)) = "BMP" Then
Call AddTable
oDoc.Tables(intTables).Cell(1, 1).Select
With Selection
.InlineShapes.AddPicture xPath & "\" & xFile, False, True
.InsertAfter vbCrLf
.MoveDown wdLine
End With
oDoc.Tables(intTables).Cell(2, 1).Select
With Selection
.Text = xFile & Chr(10)
.MoveDown wdLine
End With
' Exit out of table
oDoc.Tables(intTables).Select
With Selection
.Collapse WdCollapseDirection.wdCollapseEnd
End With
End If
xFile = Dir()
Loop
End If
End If
'Application.Selection.EndOf
' For Each oSec In oDoc.Sections
' Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
' AddHeaderToRange rng
' Next oSec
End Sub
Private Sub AddTable()
'
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=2, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
' intTables = oDoc.Tables.Count
intTables = intTables + 1
oDoc.Tables(intTables).Select
With Selection
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.Columns.PreferredWidthType = wdPreferredWidthPoints
.Columns.PreferredWidth = InchesToPoints(8.5)
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Rows.AllowBreakAcrossPages = False
.Rows.HeightRule = wdRowHeightExactly
.Rows(1).Height = InchesToPoints(6)
.Rows(2).Height = InchesToPoints(0.75)
End With
End Sub