Solved

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

Posted on 2012-03-13
4
418 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
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

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

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
 

Author Closing Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now