View Single Post
 
Old 09-14-2015, 04:58 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
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 ofgmayor has much to be proud of
Default

The following will put the selected graphics at your bookmarks, four to a document.
Put all the code in the same new module
Code:
Option Explicit

Sub InsertImages()
'Graham Mayor
Const strFile As String = "C:\Path\ImageTemplate.docx"        'The template
Dim oBM As Bookmark
Dim oDoc As Document
Dim vImages As Variant
Dim i As Long
    vImages = Split(BrowseForFiles, Chr(124))
    On Error GoTo err_Handler
    For i = LBound(vImages) To UBound(vImages) Step 4
        Set oDoc = Documents.Add(strFile)
        ImageToBM "bm1", CStr(vImages(i))
        ImageToBM "bm2", CStr(vImages(i + 1))
        ImageToBM "bm3", CStr(vImages(i + 2))
        ImageToBM "bm4", CStr(vImages(i + 3))
    Next i
lbl_Exit:
    Exit Sub
err_Handler:
    GoTo lbl_Exit
End Sub

Private Function BrowseForFiles(Optional strTitle As String) As String
'Graham Mayor
'strTitle is the title of the dialog box
Dim fDialog As FileDialog
Dim i As Long
    On Error GoTo err_Handler
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .Title = strTitle
        .AllowMultiSelect = True
        .Filters.Clear
        .Filters.Add "Image Files", "*.jpg,*.png,*.tif,*.bmp,*.gif"
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then GoTo err_Handler:
        BrowseForFiles = ""
        For i = 1 To fDialog.SelectedItems.Count
            BrowseForFiles = BrowseForFiles & fDialog.SelectedItems(i)
            If i < fDialog.SelectedItems.Count Then
                BrowseForFiles = BrowseForFiles & Chr(124)
            End If
        Next i
    End With
lbl_Exit:
    Exit Function
err_Handler:
    BrowseForFiles = vbNullString
    Resume lbl_Exit
End Function

Private Sub ImageToBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set oRng = .Bookmarks(strBMName).Range
        oRng.InlineShapes.AddPicture _
                Filename:=strValue, LinkToFile:=False, _
                SaveWithDocument:=True
        oRng.End = oRng.End + 2
        oRng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
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