Solved

Need Word Macro to Select Graphics Files and Insert Each Separated by Hard Return

Posted on 2012-03-13
4
438 Views
Last Modified: 2012-03-17
I need to be able to start a macro in a new or existing document that will:
1. Allow me to go to a folder.
2. Select individual graphics files in the folder.
3. Click on OK.
4. The graphics files would then be inserted into the document:
Graphics File 1
Paragraph Mark
Graphics File 2
Paragraph Mark
Graphics File 3
Paragraph Mark
...
Last File
Paragraph Mark
0
Comment
Question by:Alex972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37717431
Have you tried recording one? That should get you started.
0
 
LVL 1

Author Comment

by:Alex972
ID: 37731165
I recorded the following macro.

There are several limitations that I would like help fixing:
1) The folder that will be used will differ each time I use it.
2) The number of pictures chosen will vary each time I use ti.
3) The pictures come in touching other.  
    I want two or more hard returns/paragraph marks between them.
    (see Before and After Pictures below).

Sub GetPix()
'
' GetPix Macro
'
'
    Selection.InlineShapes.AddPicture FileName:="U:\Captures\11-1027-00.jpg", _
        LinkToFile:=False, SaveWithDocument:=True
    ActiveWindow.ActivePane.SmallScroll Down:=-119
End Sub

Open in new window

Before.jpg
After.jpg
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37731555
This uses the Office File dialogue to select the files which are placed after the current selection.
Sub InsertPics()
    Dim fd As FileDialog
    Dim rng As Range
    Dim ilsh As InlineShape
    
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    Dim strFileName As Variant
    
    With fd
        .Filters.Clear
        .Filters.Add "All files", "*.*"
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
        If .Show Then
            Set rng = Selection.Range
            rng.Collapse wdCollapseEnd
            For Each strFileName In .SelectedItems
                Set ilsh = ActiveDocument.InlineShapes.AddPicture(strFileName, False, True, rng)
                Set rng = ilsh.Range
                rng.Collapse wdCollapseEnd
                rng.Text = vbCr & vbCr
                rng.Collapse wdCollapseEnd
            Next strFileName
        End If
    End With

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alex972
ID: 37732578
Thanks, just what I was looking for.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question