|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Import images, resize them, and fit them to specific locations on the page
I am trying to create a macro that, when activated, imports a specific number of images from a designated folder, resizes them to specific dimensions, and places them at specific coordinates on the page (within tables, actually). The name of the image governs how they are imported into the document. For example, an image named "1" will be imported, resized, and relocated to a specific spot, an image named "2" will be imported, resized, and relocated to a different spot, and so forth.
I've been researching how to do this in Word, but am surprised that I haven't come across any solutions yet. The fact that Word disables the ability to import images when in "Record Macro" mode has been a huge roadblock for me, since I am not great at writing VBA freehand. I was able to get a similar macro working in Excel using this VBA code, but I really need to do this in Word, not Excel. Here is some code from my Excel macro: Code:
Sub MovePictures() 'Page 1, Picture 1 ActiveSheet.Pictures.Insert("[Image Filepath Here]") _ .Select Selection.ShapeRange.Width = 92.16 Selection.ShapeRange.IncrementLeft -360 Selection.ShapeRange.IncrementTop 75.75 'Page 1, Picture 2 ActiveSheet.Pictures.Insert("[Image Filepath Here]") _ .Select Selection.ShapeRange.Width = 89.28 Selection.ShapeRange.IncrementLeft -478.5 Selection.ShapeRange.IncrementTop 574 'Page 1, Picture 3 ActiveSheet.Pictures.Insert("[Image Filepath Here]") _ .Select Selection.ShapeRange.Width = 182.88 Selection.ShapeRange.IncrementLeft -709.5 Selection.ShapeRange.IncrementTop 473.25 'Page 1, Picture 4 ActiveSheet.Pictures.Insert("[Image Filepath Here]") _ .Select Selection.ShapeRange.Width = 184.32 Selection.ShapeRange.IncrementLeft -515.25 Selection.ShapeRange.IncrementTop 472 End Sub Any help would be very much appreciated! |
#2
|
||||
|
||||
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
macropod, thanks for the quick response. I think this is the right direction, but what I need might be a little more complicated.
I'm working from a template document (see attached mockup). As a template document, it has a pre-filled document header, table headers, table text, etc. (here represented with "lorem ipsum" filler text). What I want the macro to do is fill images in the spaces listed with "IMAGE #" from a designated folder containing all of the images (named a universal way so IMAGE 1, IMAGE 2, IMAGE 3, etc. is always clear). The macro example you provided seems to assume you're working from a blank document. Instead of creating the table and everything from the ground up using a macro, is it possible to create a macro that recognizes where all instances of "IMAGE #" fall on the page(s) and import, resize, and insert the appropriate images in its place? Assuming I will need to have the macro create the table from the ground up, can I tell the macro to insert all of the pre-filled "lorem ipsum" text along with the images? Appreciate your help; let me know if you have questions. |
#4
|
||||
|
||||
A macro could certainly add images to an existing table, but it's not apparent from what you've posted how a macro would determine which image(s) belongs in which cell of the table.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Good question. I had in mind that maybe the macro could locate specific text in the document (e.g., "IMAGE 1") and replace the text with an image. The image would be imported from a designated folder, where all of the file names are named in such a way that the macro knows which to grab. For example, the imaged named "1" would always be inserted over the "IMAGE 1" text on the page. This particular template always uses thirteen images, as you can see in the attachment showing the image source folder.
If that's not possible, is there a way to tell the macro to locate a specific row/column combination? If so, row 2 column 2 should use the "IMAGE 1" image, row 3 column 2 should use the "IMAGE 1" image, row 4 column 2 should use three images: "IMAGE 2" "IMAGE 3" and "IMAGE 4" in that order, left to right. If neither of those options are possible to accomplish with a macro . . . I'll have to think on it some more. |
#6
|
||||
|
||||
If the document and images will be kept in the same folder, here's a way to do it that: (a) doesn't require a macro; (b) updates the document just by changing the image in the folder; and (c) keeps the document's file size to a minimum.
1. Press Alt-F9 to toggle Word's field code display 'on'. 2. Press Ctrl-F9 twice to create a pair of nested fields wherever you want the first image to go. They'll look like { { } }. 3. Fill in the fields, so you end up with {INCLUDEPICTURE "{FILENAME \p}/../Image1.png"}. Change the 'png' to whatever extension your image has. 4. Copy the field code from step 3 and paste it to wherever you want the other images to appear, changing the image # in each field to match. 5. Save the document to the folder the images are stored in. 6. Press Alt-F9 to toggle Word's field code display 'off'. 7. Press Ctrl-A, F9 to update the display. If you change one of the images in the folder, then press Ctrl-A, F9 to update the display, Word will replace the image in the document. If you copy the document to another folder with its own set of numbered images (same image names & extensions) and press Ctrl-A, F9 to update the display, Word will replace all the images in the document. Let me know if that suits your needs; otherwise I can write a macro.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Awesome! Let me try this out and get back to you.
One thing though: the source images are virtually always the wrong size for the table (too big, generally). Does this method allow me to shrink each image (either by the width or the height, to retain the aspect ratio) so they always fit correctly? |
#8
|
||||
|
||||
If you give the table cells a fixed row height and column width (which even a macro would require), the inserted images should rescale to fit the available space at the correct aspect ratio.
In your screenshot, it appears one cell is to contain three images. You should split that cell into three, using whatever configuration is appropriate, so each image has its own cell to constrain its dimensions.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
WOW! That worked like a charm! Thank you so much for helping me with this; I really appreciate it. I don't think I need any other help from here, but I'll let you know if I do.
|
Tags |
image insert |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Complex VBA Code to Resize Alternating Images at the End of a Mail Merge | macropodjr | Mail Merge | 5 | 09-11-2017 11:10 AM |
Add pictures in specific parts of the document and resize them automatically - MS Word 2013 | Andrew_G93 | Word | 6 | 11-10-2015 09:00 AM |
Help Creating Macro to Crop/Resize Images | ineedmacrohelp | Word VBA | 1 | 08-06-2015 06:46 AM |
Help with VBA to resize images | Yuffster | Word VBA | 2 | 01-30-2015 06:05 AM |
WORD Macro - import picture - resize - position - page break - loop | Nano07 | Word VBA | 2 | 11-02-2011 05:14 AM |