Microsoft Office Forums Import images, resize them, and fit them to specific locations on the page

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2018, 01:51 PM
hysterical.useless hysterical.useless is offline Import images, resize them, and fit them to specific locations on the page Windows 10 Import images, resize them, and fit them to specific locations on the page Office 2016
Novice
Import images, resize them, and fit them to specific locations on the page
 
Join Date: Jan 2018
Posts: 12
hysterical.useless is on a distinguished road
Default 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
I achieved this in Excel by turning on the "Record Macro" feature and, one at a time, importing all images from a specific cell location, resizing them from that location, then moving them to the appropriate spots with my arrow keys so I could calculate the exact IncrementLeft and IncrementTop coordinates for each image. Unfortunately, I don't think this same approach will work for Word, given the more rigid nature of a Word document relative to using images.

Any help would be very much appreciated!
Reply With Quote
  #2  
Old 01-26-2018, 05:24 PM
macropod's Avatar
macropod macropod is online now Import images, resize them, and fit them to specific locations on the page Windows 7 64bit Import images, resize them, and fit them to specific locations on the page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,671
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

See: https://www.msofficeforums.com/word-...html#post47919
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 01-29-2018, 03:04 PM
hysterical.useless hysterical.useless is offline Import images, resize them, and fit them to specific locations on the page Windows 10 Import images, resize them, and fit them to specific locations on the page Office 2016
Novice
Import images, resize them, and fit them to specific locations on the page
 
Join Date: Jan 2018
Posts: 12
hysterical.useless is on a distinguished road
Default

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.
Attached Images
File Type: png template example.PNG (35.1 KB, 17 views)
Reply With Quote
  #4  
Old 01-29-2018, 03:12 PM
macropod's Avatar
macropod macropod is online now Import images, resize them, and fit them to specific locations on the page Windows 7 64bit Import images, resize them, and fit them to specific locations on the page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,671
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #5  
Old 01-29-2018, 03:47 PM
hysterical.useless hysterical.useless is offline Import images, resize them, and fit them to specific locations on the page Windows 10 Import images, resize them, and fit them to specific locations on the page Office 2016
Novice
Import images, resize them, and fit them to specific locations on the page
 
Join Date: Jan 2018
Posts: 12
hysterical.useless is on a distinguished road
Default

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.
Attached Images
File Type: png picture repository.PNG (3.1 KB, 18 views)
Reply With Quote
  #6  
Old 01-29-2018, 04:42 PM
macropod's Avatar
macropod macropod is online now Import images, resize them, and fit them to specific locations on the page Windows 7 64bit Import images, resize them, and fit them to specific locations on the page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,671
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #7  
Old 01-29-2018, 05:31 PM
hysterical.useless hysterical.useless is offline Import images, resize them, and fit them to specific locations on the page Windows 10 Import images, resize them, and fit them to specific locations on the page Office 2016
Novice
Import images, resize them, and fit them to specific locations on the page
 
Join Date: Jan 2018
Posts: 12
hysterical.useless is on a distinguished road
Default

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?
Reply With Quote
  #8  
Old 01-29-2018, 05:34 PM
macropod's Avatar
macropod macropod is online now Import images, resize them, and fit them to specific locations on the page Windows 7 64bit Import images, resize them, and fit them to specific locations on the page Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,671
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #9  
Old 01-30-2018, 04:27 PM
hysterical.useless hysterical.useless is offline Import images, resize them, and fit them to specific locations on the page Windows 10 Import images, resize them, and fit them to specific locations on the page Office 2016
Novice
Import images, resize them, and fit them to specific locations on the page
 
Join Date: Jan 2018
Posts: 12
hysterical.useless is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
image insert

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import images, resize them, and fit them to specific locations on the page 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
Import images, resize them, and fit them to specific locations on the page Help Creating Macro to Crop/Resize Images ineedmacrohelp Word VBA 1 08-06-2015 06:46 AM
Import images, resize them, and fit them to specific locations on the page Help with VBA to resize images Yuffster Word VBA 2 01-30-2015 06:05 AM
Import images, resize them, and fit them to specific locations on the page WORD Macro - import picture - resize - position - page break - loop Nano07 Word VBA 2 11-02-2011 05:14 AM


All times are GMT -7. The time now is 03:47 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft