A user needed to resize images in incoming email.
A few days earlier another user posted a sample macro he needed help with; his macro resized images. I put bits of his macro together with my Format Selected Text macro to resize selected images in a message.
To use this macro, you need to open the message and enable Editing.
To test the macro, select a message containing large images and press Ctrl+C to copy the message then press Ctrl+V several times to make copies of the message, so you have plenty of messages to work with as you determine the best size for your needs.
- Open the message.
- On the opened message's ribbon or menus: Go to Actions, Edit Message (Actions is in the Move section of the ribbon)
- Select the images(s) or entire message.
- Run the macro.
This screenshot shows the original image size and the size after running the macro, with the picSize value set to 13:
The picture size (in CM) is set in this line:
picSize = 13
You'll need to set a reference to the Word Object Model in the VB Editor's Tools > References.
Public Sub ResizeImagesReceivedMail() Dim objItem As Object Dim objInsp As Outlook.Inspector ' Add reference to Word library ' in VBA Editor, Tools, References Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection On Error Resume Next Dim targetHeight As Integer Dim oShp As Shape Dim oILShp As InlineShape Dim picSize As Variant ' make all images (both inline and floating) ' 13 cm wide while preserving aspect ratio picSize = 13 'Reference the current Outlook item Set objItem = Application.ActiveInspector.CurrentItem If Not objItem Is Nothing Then If objItem.Class = olMail Then Set objInsp = objItem.GetInspector If objInsp.EditorType = olEditorWord Then Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection With objSel ' Formatting code goes here For Each oShp In objSel.ShapeRange With oShp .LockAspectRatio = msoTrue .Height = AspectHt(.Width, .Height, CentimetersToPoints(picSize)) .Width = CentimetersToPoints(picSize) End With Next For Each oILShp In objSel.InlineShapes With oILShp .LockAspectRatio = msoTrue .Height = AspectHt(.Width, .Height, CentimetersToPoints(picSize)) .Width = CentimetersToPoints(picSize) End With Next End With End If End If End If Set objItem = Nothing Set objWord = Nothing Set objSel = Nothing Set objInsp = Nothing End Sub Private Function AspectHt(ByVal origWd As Long, ByVal origHt As Long, ByVal newWd As Long) As Long If origWd <> 0 Then AspectHt = (CSng(origHt) / CSng(origWd)) * newWd Else AspectHt = 0 End If End Function
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.
- Go to Tools, References menu.
- Locate the object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
More information as well as screenshots are at How to use the VBA Editor