Macro to crop and resize images in Word

macOS

Preface

This article is mostly intended for users of Ekahau Site Survey Pro generating report documentation.

I encourage you to clean-up, manipulate and prepare your floor plans before importing them into Ekahau. This will save you from a headache when it comes to report-writing time. Image manipulation is much easier before we create the Ekahau project.

However...

Main Attraction

We have all been there... right?

The survey is complete, we have looked through and analysed the collected data. It's time to smash out that report document, only, the coverage map visualisations have a ton of white space surrounding them and the floor plan is minuscule in the centre of the page.

It's a PITA, but, by creating a macro within Word we can automate (and dramatically speed up) the image cropping and resize actions.

  1. Create and run your Ekahau report template
  2. Word > Preferences
    Change measurement unit to points
  3. Tools > Macro > Visual Basic Editor
    double click "This Document"
    copy and paste the VBA script below
Sub CropAndResize()
' This macro crops and resizes images
' The units are 'points'
Dim oILS As InlineShape
Set oILS = Selection.InlineShapes(1)
With oILS
.PictureFormat.CropLeft = 100
.PictureFormat.CropTop = 100
.PictureFormat.CropRight = 100
.PictureFormat.CropBottom = 100
End With
With oILS
.LockAspectRatio = True
' .Height = 260
' .Width = 450
End With
lbl_Exit:
Exit Sub
End Sub
  1. Click "Save" in the top-left
  2. You will likely get an error message, cancel
  3. Save the Document / Macro in the special ".docm" format
  4. Next, create a shortcut for the macro
    Tools > Customise Keyboard
  5. Assign a keyboard combo of your choosing
  6. Test the macro on an image
  7. The crop values are absolute! Running the macro a second time will not affect the image cumulatively.
  8. Tools > Macro > Visual Basic Editor
    Make the changes
    Save in the top left
    Close the window
  9. Run the macro again using your shortcut
  10. The macro can also be used to resize the image
    To define the width or height using the macro, remove the apostrophe ' from the appropriate line width or height and set the value in points
  11. If you are unsure what the point value should be, consider the following
    cropping an image using the macro
    resize the image manually within Word
    select the "Picture Format" within the ribbon
    take note of the width or height
    enter the value into the macro and run again

The macro will need to be applied to each image that requires manipulation, depending on your project, you may need to alter the macro for different shape floors within your report output.When designing your report template, you can use the visualisation tag:

<#"visualization": {"resolution-width": "-1"}#>

This tag sets the resolution of the rendered visualization. This time the unit is in pixels. This tag is often used to reduce the resolution and save space in the resulting generated report...

That is NOT what we are doing here, set to negative 1, to force use the original resolution of the map image.

This should allow you to crop and enlarge the image within Word without it becoming grainy.


Windows Users

Create and run your Ekahau report template

  1. File > Options > Advanced > Display
    Change:
    Show measurements in units of to: Points
  2. OK
  3. (In the Ribbon) View > Macros > View Macros
  4. Type "CropAndResize" into the Macro Name field
    Click "Create"
  5. We are now in the MSWord Visual Basic Editor
    Copy and paste the VBA script below in the editor in between the lines of Sub "CropAndResize" and "EndSub"
    Commented out lines within VBA scripts begin with ‘'’
' This macro crops and resizes images
' The units are 'points'
Dim oILS As InlineShape
Set oILS = Selection.InlineShapes(1)
With oILS
.PictureFormat.CropLeft = 100
.PictureFormat.CropTop = 100
.PictureFormat.CropRight = 100
.PictureFormat.CropBottom = 100
End With
With oILS
.LockAspectRatio = True
' .Height = 260
' .Width = 450
End With
lbl_Exit:
Exit Sub
  1. Click "Save" in the top-left
  2. You may get an error message, click "cancel"
  3. Save the Document / Macro into the special ".docm" format
  4. Once the macro is saved and we are returned to looking at the Word document
    Create a shortcut for the macro
    File > Options > Customise Ribbon
    At the bottom of the screen select Keyboard Shortcuts "Customise"
    Scroll down through the categories (on the left) to Macros
  5. Make sure the Macro Name is visible and selected in your Macros box (on the right)
  6. Assign a keyboard combo of your choosing in ‘Press new shortcut key:’
    Click "Assign"
    Then you can close the Options windows
  7. Test the macro on an image
    by selecting the image and using the newly created keyboard shortcut
  8. Please Note: The crop values used by this macro are absolute! Running the macro a second time will not affect the image cumulatively.
  9. If the crop amount is not sufficient, we need to go back to the macro and make some changes.
  10. (In the Ribbon) View > Macros > View Macros
    Edit
  11. This time, all we need to do is alter the crop values
    Once complete
    Save in the top left
    Close the window
  12. Run the macro again using your shortcut

Big thanks to Ameya Ramadurgakar for helping me with the Windows instructions.

Credit goes to Numerous Google search results for this solution, including this forum post