VBA - Word Photo Album - insert new table

justhumm

New Member
Joined
Aug 1, 2013
Messages
20
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!

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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Nevermind...this needs to be cleaned up, but basically does what I want...

I needed to add in a new paragraph (or two) after the table.

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'
' https://stackoverflow.com/questions/34794656/vba-insert-multiple-table-in-documnet
'
    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

End Sub

Private Sub AddTable()
'
    oDoc.Paragraphs.Add
    oDoc.Paragraphs.Last.Range.Select
    
    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(5.875)
        .Rows(2).Height = InchesToPoints(0.625)
    End With
    
End Sub

Private Sub SetPage()
    With oDoc.PageSetup
        .LineNumbering.Active = False
        '-----
        .PageWidth = InchesToPoints(8.5)
        .PageHeight = InchesToPoints(11)
        .Orientation = wdOrientLandscape
        '-----
        .TopMargin = InchesToPoints(0.75)
        .BottomMargin = InchesToPoints(0.75)
        .LeftMargin = InchesToPoints(0.75)
        .RightMargin = InchesToPoints(0.5)
        .Gutter = InchesToPoints(0)
        .GutterPos = wdGutterPosLeft
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
        '-----
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
    End With
    
    oDoc.Select
    With Selection
        .Font.Name = "calibri"
        .Font.Size = 11
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,956
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top