Every week John needs to create an invoice and insert several scanned images into a Word document. He wonders if there is a way to put all of the JPG images from a particular folder into the current Word document.
There are a couple of ways you can do this. One way is to simply select all the graphics and insert them in one step:
That's it; Word inserts all the graphics in the document. You'll want to be careful doing this, because if there are a lot (more than, say, 100) graphics in the folder or if the graphic files are quite large, you can slow Word to a crawl or hang it completely. If the folder contains more than just graphics or you want only some of the graphic files, you can hold down the Ctrl key as you use the mouse to select graphics in step 4. The point is that whatever files you have selected in step 4, those are the files that Word inserts in your document.
A variation on this approach involves the use of Windows. Follow these general steps:
At this point, all the images you selected in step 4 are inserted in the Word document, the same as in the earlier method.
If you use either of the foregoing methods, you'll need to resize images and move them around, as necessary, so your document appears the way you desire.
Of course, if you need to insert whole groups of images on a routine basis, then going through these steps can become tiresome after a while. Tedium is often relieved through the use of a macro, and this case is no exception. The following macro can be used to display a dialog box (similar to those used in the earlier methods) where you can select the files you want. When you close the dialog box, any images you selected within the dialog box are inserted in your document, each in its own paragraph.
Sub InsertImages() Dim doc As Word.Document Dim fd As FileDialog Dim vItem As Variant Dim mg1 As Range Dim mg2 As Range Set fd = Application.FileDialog(msoFileDialogFilePicker) Set doc = ActiveDocument With fd .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 .FilterIndex = 1 If .Show = -1 Then For Each vItem In .SelectedItems Set mg2 = ActiveDocument.Range mg2.Collapse wdCollapseEnd doc.InlineShapes.AddPicture _ FileName:=vItem, _ LinkToFile:=False, SaveWithDocument:=True, Range:=mg2 Set mg1 = ActiveDocument.Range mg1.Collapse wdCollapseEnd mg1.Text = vbCrLF & vbCrLf Next vItem End If End With Set fd = Nothing End Sub
If you don't want to bother with selecting folders or files, you can use an even simpler macro. The following pulls all JPG files from a specified folder and places them in the current document. Each image is in its own paragraph.
Sub GetPictures() Dim sPic As String Dim sPath As String sPath = "c:\myfolder\" sPic = Dir(sPath & "*.jpg") Do While sPic <> "" Selection.InlineShapes.AddPicture _ FileName:=sPath & sPic, _ LinkToFile:=False, SaveWithDocument:=True sPic = Dir Selection.TypeParagraph Selection.TypeParagraph Loop End Sub
To use the macro, just make sure you change the value assigned to sPath so that it represents the folder you want.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10726) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Inserting Multiple Graphics in a Document.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Got a bunch of graphics in a document that need replacing? (Perhaps you need to replace an old logo with a new one.) Word ...
Discover MoreEver wonder why a border around a graphic doesn't print the way it looks on the screen? There are several ways to add and ...
Discover MoreIf you have a document with many graphics, you know that Word can sometimes be slow in displaying all those graphics. ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-06-23 13:57:27
Anne
Hi there... I was wondering if there is a way to add formatting that would apply to each photo? Trying to figure out how make each photo inline with text, centered, at 50% is height.
Here is the code that works for adjusting height:
With oImage
.LockAspectRatio = msoTrue
.Height = .85 * .Height
Set oRng = .ConvertToShape
End With
and the code I've found (using the Macro Recorder) for adjusting the paragraph:
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
2020-06-23 13:43:43
Anne
Hi there... I was wondering if there is a way to add formatting that would apply to each photo? Trying to figure out how make each photo inline with text, centered, at 50% is height.
Here is the code that works for adjusting height:
With oImage
.LockAspectRatio = msoTrue
.Height = .85 * .Height
Set oRng = .ConvertToShape
End With
and the code I've found (using the Macro Recorder) for adjusting the paragraph:
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
2019-04-28 07:10:12
Pablo
Hi,
I have an already made Word template I need to insert 136 unique images. Is there a way to batch all of these images into the template without individually having to manually drag and drop/copy and paste every image into the template??
Thanks,
-P
2018-08-26 11:50:48
ADEEL QAMAR
Hello dear, i am new to your blog and a nebiew in vba. I tried the coding and it works great. I want to say thanks for great help. Further can you add coding for adding image name as title before image. This will be a great help.
Thanks in advance ...
2017-10-16 13:31:53
Fil
FYI this does not work for office 2000 or earlier. FileDialog does not exist before office 2002. Office 2000, even patched with Sp3 will crash out with "compile error - user defined type not defined. "
2017-07-28 08:56:16
James M.
Hi, do you have code for doing a similar task but instead of graphics, embedding objects, specifically all the files in a defined folder. The file type being .pdf?
For example something similar to:
ActiveDocument.InlineShapes.AddOLEObject _
Set listfiles = fso.GetFolder(Folderpath).Files
ClassType:="Excel.Sheet", DisplayAsIcon:=True, _
Range:=ActiveDocument.Paragraphs(2).Range
let me know if you can come up with anything,
Thanks,
James
2015-10-13 10:43:01
Fredrik Olsson
Hi,
If I select some images (among many images) in a certain order (not alpabetic), is there a way to keep this order while inserting them into the document? Preferably as a macro.
Thanks.
2014-12-10 22:50:53
Debbie Douglas
I am creating a Souvenir Booklet in Word 2013 (not my first choice,but that is the program other members of the church are familiar with. My question: Need to add say 5 images to one page. Title, Our Treasures who have gone home to Heaven and then scatter images of those who have gone home in an interesting way on the page. I have some turned, etc. But it was very hard, thinking there is an easier way. Thanks for your help.
2012-10-26 07:23:23
Fernando C.
Isn't possible adapt this macro to insert each photo in a table with 1 L and 2 C? The 1st cell have the number of photo and the photo, the 2nd cell suits to make a description (limited to 600 characters).
This table must be repeated for each photo.
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments