×
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 – Choosing the Right Spring Loaded Connector
In today’s cost-sensitive world, designers are often driven to specify the lowest cost solution for every aspect of their designs to ensure that their solution is competitively priced and their company remains profitable. However, specifying a low-cost, low-quality connector solution can result in premature failure, considerable re-work costs and damage to reputations. Download Now
eBook – Own the Lifecycle: Sustainable Business Transformation
Increasingly, product and services companies are seeking more information and control in the operational lifecycle of their products, including service and use. Better information about the operational lifecycle, and the ability to use that information, requires more than just unstructured data flowing back from products in the field. 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