View Single Post
 
Old 11-13-2019, 11:31 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try the following, however you may find Photo Gallery Add-in Template useful

Code:
Sub InsertMultipleImagesFixed()
Dim fd As FileDialog
Dim oTable As Table
Dim oCell As Range
Dim i As Long
Dim oShape As InlineShape
Dim scale_Factor As Long
Dim max_height As Single
Dim oCC As ContentControl
    'define resize constraints
    max_height = 275

    'add a 1 row 1 column table to take the images
    Set oTable = Selection.Tables.Add(Selection.Range, 1, 1)
    '+++++++++++++++++++++++++++++++++++++++++++++
    'oTable.AutoFitBehavior (wdAutoFitFixed)
    oTable.Rows.Height = CentimetersToPoints(4)
    oTable.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
    '++++++++++++++++++++++++++++++++++++++++++++++

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .TITLE = "Select image files and click OK"
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png; *.wmf"
        .FilterIndex = 2
        If .Show = -1 Then

            For i = 1 To .SelectedItems.Count
                'select cell
                Set oCell = ActiveDocument.Tables(1).Cell(i, 1).Range
                oCell.End = oCell.End - 1
                'insert image
                Set oShape = oCell.InlineShapes.AddPicture(FileName:= _
                                                           .SelectedItems(i), LinkToFile:=False, _
                                                           SaveWithDocument:=True, Range:=oCell)

                'resize image
                If oShape.Height > max_height Then
                    scale_Factor = oShape.ScaleHeight * (max_height / oShape.Height)
                    oShape.ScaleHeight = scale_Factor
                    oShape.ScaleWidth = scale_Factor
                End If

                'center content
                oCell.ParagraphFormat.Alignment = wdAlignParagraphCenter

                'insert caption below image

                Set oCell = ActiveDocument.Tables(1).Cell(i, 1).Range
                oCell.End = oCell.End - 1

                oCell.Collapse 0
                oCell.Text = vbCr & vbCr
                oCell.Collapse 0

                Set oCC = oCell.ContentControls.Add
                With oCC
                    .Type = wdContentControlRichText
                    .TITLE = "Image " & i
                    .Tag = .TITLE
                    '.LockContentControl = True
                End With
                If i < .SelectedItems.Count Then oTable.Rows.Add
            Next i
        End If
    End With
    Set oShape = Nothing
    Set oTable = Nothing
    Set oCell = Nothing
    Set fd = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote