Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Photo Album - insert new table

Status
Not open for further replies.

justhumm

Structural
May 2, 2003
112
I'm trying to put together a photo album macro for Word.

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 folder
[indent]Insert New (2x1) Table[/indent]
[indent]Exit out of Table[/indent]

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()
'
' [URL unfurl="true"]https://www.extendoffice.com/documents/word/5451-insert-picture-with-filename-in-word.html[/URL]
'
    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
 
Replies continue below

Recommended for you

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()
'
' [URL unfurl="true"]https://www.extendoffice.com/documents/word/5451-insert-picture-with-filename-in-word.html'[/URL]
' [URL unfurl="true"]https://stackoverflow.com/questions/34794656/vba-insert-multiple-table-in-documnet[/URL]
'
    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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor