Word Macro to paste MS Office Drawing / TIFF image as PNG

Posted

I really don’t know how to explain today’s post. In short: I want to copy drawings and images out of Microsoft Office in PNG format. On macOS, copy-and-paste seems to prefer the TIFF format, resulting in large Office files. I’d rather the images be converted to PNG, preserving transparency but providing high compression. However, this is more complicated than it seems, because of limitations in Office and PowerPoint in particular...

Danger! Macros! Internet code! Risk! Disclaimer! Please don’t copy what I do here. This code is here for my own use!

The problem

When I copy drawings or images from Microsoft PowerPoint on Mac, the format put on the clipboard seems to be PDF, TIFF or Microsoft Office Drawing Object only:

Microsoft Powerpoint Paste Special with TIFF only

However, I am after is the drawing or image as a PNG, that I can, for example, paste into a Word document (or from Word to PowerPoint too). Otherwise, I land up with very large files due to the very low compression of TIFF images.

Unlike PowerPoint, Microsoft Word is able to convert the TIFF and paste it as PNG - I can’t seem do this in PowerPoint itself!

So, I land up with this multi-step process:

  1. In PowerPoint, select the objects and Copy (Command-C).
  2. Open Word, hit Paste (Command-V). If at this point you hit Paste Special you’ll see the clipboard only has PDF, TIFF and Microsoft Office Drawing Object formats available (screenshot above).
  3. Then select the just pasted image, and Cut.
  4. Hit Paste Special (Control>-Command-V) and select PNG. Now, you notice that the clipboard has PNG and JPEG formats too!

Microsoft Word Paste Special with JPEG and PNG

BTW I previously described a shell script to convert TIFF to PNG in an existing PPTX presentation.

The workaround

So, I decided to write a simple Visual Basic for Applications (VBA) script (a.k.a. but a macro) to automate this process. Navigate from the Word menu: Tools > Macro > Visual Basic Editor. I have the code below under Microsoft Word Objects > ThisDocument.

The macro basically goes through steps #2 to #4 above. Additionally, the macro selects the newly pasted PNG and copies is to the clipboard, so that now I am free to paste it anywhere. And finally, the macro closes the document without saving it.

Private Sub Document_Open()
On Error GoTo Err
    Selection.PasteSpecial Link:=False, DataType:=16, Placement:=wdInLine
    Selection.MoveLeft wdCharacter, 1, wdExtend
    Selection.Cut
    Selection.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine
    Selection.MoveLeft wdCharacter, 1, wdExtend
    Selection.CopyAsPicture
Err:
    ActiveDocument.Close False
End Sub

Save As... a Word Macro-enabled Document (.docm), and it should look like this:

Microsoft Word Visual Basic VBA Macro

When opening this Word document, Word prompts a warning message regarding to either execute or disable the macro:

Microsoft Word Document Contains Macros Warning@2x.png

  • Hit the first button to continue - the core automatically runs (on Document_Open). Any errors are quietly ignored (On Error GoTo), and you will end up with nothing done.
  • Hit the last button and you can open Word as normal to edit the Macro, but not run it.

The automation

I previously posted some of my Touch Bar Quick Actions. Among them is this: Using a Quick Action to open the Word Document that contains the macro above to “convert” what is in the clipboard to PNG format:

Automator Quick Action to Open a Document with Microsoft Word

So, make sure you have a Microsoft Office Drawing Object in the clipboard (e.g. Copy from Powerpoint), and hit the Quick Action. Answer the warning message and it’ll seem like nothing happens because the document is opened and closed very quickly. Now go ahead and Paste the resulting PNG somewhere (e.g. back in Powerpoint).

Conclusion

Well, hopefully this won’t be needed in the future, and Office will default to JPG or PNG instead of TIFF. For now, I do use this once in a while. Again, I don’t recommend anyone run any untrusted code from the Internet, but perhaps this is educational.

Finally, read this post to convert TIFFs into PNGs in an existing PPTX - in involves unpacking the ZIP-compressed PPTX, using built-in macOS tools to convert TIFF to PNG, and re-packing as a PPTX. Mileage varies, does not work for some images can results in errors. Not bothered to fix, and again, not recommended for anyone except me!