×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Word Photo Album - insert new table

Word Photo Album - insert new table

Word Photo Album - insert new table

(OP)
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 --> Pseudo-Code

Format Page
Select Folder
For each image in folder
Insert New (2x1) Table
Exit out of Table

CODE --> VBA

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 

RE: Word Photo Album - insert new table

(OP)
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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

White Paper - The Criticality of the E/E Architecture
Modern vehicles are highly sophisticated systems incorporating electrical, electronic, software and mechanical components. Mechanical systems are giving way to advanced software and electronic devices, driving automakers to innovate and differentiate their vehicles via the electric and electronic (E/E) architecture. As the pace of change accelerates, automotive companies need to evolve their development processes to deliver and maximize the value of these architectures. Download Now
White Paper - Model Based Engineering for Wire Harness Manufacturing
Modern cars, trucks, and other vehicles feature an ever-increasing number of sophisticated electrical and electronic features, placing a larger burden on the wiring harness that enables these new features. As complexity rises, current harness manufacturing methods are struggling to keep pace due to manual data exchanges and the inability to capture tribal knowledge. A model-based wire harness manufacturing engineering flow automates data exchange and captures tribal knowledge through design rules to help harness manufacturers improve harness quality and boost efficiency. Download Now
White Paper - Modeling and Optimizing Wire Harness Costs for Variation Complexity
This paper will focus on the quantification of the complexity related costs in harness variations in order to model them, allowing automated algorithms to optimize for these costs. A number of real world examples will be provided as well. Since no two businesses are alike, it is the aim of this paper to provide the foundational knowledge and methodology so the reader can assess their own business to model how variation complexity costs affect their business. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close