Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-13
4
Medium Priority
?
451 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:Alex Campbell
  • 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:Alex Campbell
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 2000 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:Alex Campbell
ID: 37732578
Thanks, just what I was looking for.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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.

772 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