# Fitting images to document's margins in a docx file

Super User Asked by Tal Galili on January 13, 2021

I’ve got a docx file with many figures, all do not fit the margins of the document. I can manually adjust the sizes of the figures in the file, but would love to have some way to automate this (either from Word, from a command line tool, or any other means).

(PS: this is a follow-up to this question)

Answered by Victor Onrust on January 13, 2021

Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.

Now it's working with any available page width (don't forget to fix height, if needed, too) :

Sub PicturesFitPageWidth()
' ResizePic Macro
' Resizes an image

Shapes = ActiveDocument.Shapes.Count
InLines = ActiveDocument.InlineShapes.Count
'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.

'Calculate usable width of page

With ActiveDocument.PageSetup
WidthAvail = .PageWidth - .LeftMargin - .RightMargin
End With

For ShapeLoop = 1 To Shapes
MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
End If
Next ShapeLoop
'Loops through all shapes in the document.  Checks to see if they're too wide, and if they are, resizes them.

For InLineLoop = 1 To InLines
MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
End If
Next InLineLoop
'Loops through all shapes in the document.  Checks to see if they're too wide, and if they are, resizes them.

End Sub


Answered by WebComer on January 13, 2021

You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.

Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.

Sub ResizePic()
' ResizePic Macro
' Resizes an image

Shapes = ActiveDocument.Shapes.Count
InLines = ActiveDocument.InlineShapes.Count
'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.

RightMar = ActiveDocument.PageSetup.RightMargin
LeftMar = ActiveDocument.PageSetup.LeftMargin
PaperType = ActiveDocument.PageSetup.PaperSize
PageLayout = ActiveDocument.PageSetup.Orientation
'Sets up variables for margin sizes, paper type, and page layout.
' This is used to find the usable width of the document, which is the max width for the picture.

If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
End If
'Identifies the usable width of the document, based on margins and paper size.

For ShapeLoop = 1 To Shapes
MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
End If
Next ShapeLoop
'Loops through all shapes in the document.  Checks to see if they're too wide, and if they are, resizes them.

For InLineLoop = 1 To InLines
MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
End If
Next InLineLoop
'Loops through all shapes in the document.  Checks to see if they're too wide, and if they are, resizes them.

End Sub


Answered by Kelly Tessena Keck on January 13, 2021

## Related Questions

### Keyboard unresponsive for first minute after booting Windows 8.1

3  Asked on November 29, 2021 by mildwolfie

### VPN server behind a NAT without port forwarding

1  Asked on November 29, 2021 by testvpn

### ffmpeg – Overlay one video on to another

1  Asked on November 29, 2021 by nisarg

### Is it possible to force print a PDF file borderless?

2  Asked on November 29, 2021 by sjonteflon

### How to remove all line breaks from text file using Batch

2  Asked on November 29, 2021 by user3754804

### How to disable Ctrl+Alt+Left/Right on KDE?

1  Asked on November 29, 2021 by jos-roberto-arajo-jnior

### Automatically mount and copy files to USB drive [Raspbian Buster Lite, headless]

1  Asked on November 29, 2021

### Avoiding duplicate MySQL queries in different processes

1  Asked on November 29, 2021 by murilo-schmalfuss

### Using ffmpeg to apply mpdecimate and recreating the folder structure with fixed files to another partition

0  Asked on November 29, 2021 by luiza

### Some problems with nginx rewrites – forwarding request from folder to subdomain , then processing it further

1  Asked on November 29, 2021

### Modifier key to change the action of the side scroll wheel on Logitech MX Master 3 mouse

2  Asked on November 29, 2021

### How do you arrange terminal color sequences into human friendly color grades

1  Asked on November 29, 2021

### Why is the BIOS Update option missing from my HP Laptop?

3  Asked on November 29, 2021 by ng-newbie

### VLC only in Windows 7 system tray?

5  Asked on November 27, 2021 by battistis

### How to edit a read-only document in LibreOffice?

5  Asked on November 27, 2021 by testuser16418

### Removing ANSI color codes from text stream

15  Asked on November 27, 2021

### How can I reverse mouse movement (X & Y axis) system-wide? (Win 7 x64)

5  Asked on November 27, 2021 by scivitri

### Windows shows smaller size of hard drive after cloning partitions

7  Asked on November 27, 2021

### How to find and replace string data in text file

4  Asked on November 27, 2021 by user354113

### Checkout/in Excel Workbooks from SharePoint with VBA

1  Asked on November 27, 2021 by iron-man