I wrote an article last year about Swapping Images in a #Visio Shape manually, but I want to be able to do this whenever a Shape Data value changes. This is because I use I invariably use shapes linked to data. I also want to be able to have multiple images within a single grouped shape, and all of them changing when their referenced Shape Data values change. I believe that this will be more adaptable for a lot of scenarios than trying to repurpose Data Graphic Icon Sets ( see Make Your Own Visio Data Graphic Icons Sets … automatically). I also discovered that the Shape.ChangePicture(…) function can just as easily work with urls as it can with network file paths, so even more possibilities are opened up! For example, the Visio Online JavaScript API has the ability to overlay an image (see ShapeView.addOverlay(…)).
So, I have created some macros to provide quick and easy selection, positioning and updating of images within a group shape.
I decided to present this article with VBA code rather than my preferred C# so that a stencil could be downloaded to provide instant usability, rather than having to install an add-in. It can be download from Image Utils.vssm, and should then be placed in to My Shapes folder, so that it becomes available to open in Visio. When it is opened, then the macros will need to be enabled.
There are just five public macros:
- Img_Utils.modImgs.AnchorImageInGroup – Set anchor option of an image shape within a group shape
- Img_Utils.modImgs.LockImageInGroup – Set lock options of an image shape within a group shape
- Img_Utils.modImgs.SelectReplaceImageInGroup – Allows for manual selection of an image file to replace an existing image shape within a group shape
- Img_Utils.modImgs.SetImageFormulas – Set the ShapeSheet formulas of an image shape within a group shape
- Img_Utils.modImgs.SizeImageInGroup – Set resize options of an image shape within a group shape
The SelectReplaceImageInGroup macro uses a references to the Excel object library to provide the Application.FileDialog(..) method, which is not available in the Visio object library….
Also, the Microsoft Scripting Runtime is referenced, and the code is digitally signed.
Here is a 16 minute video of the code in action, and an explanation of how it works! (Yes there is sound…).
Here are the sample Visio documents from the videos above:
Flags of the World.vsdx
Replacing Person Photos By Data.vsdx
Each of these documents require the Image Utils.vssm stencil to be open in the Visio UI, and Visio Professional or Visio Online Plan 2 is required for the Link to Data functionality, however the macros will work in Visio Standard too.
These are the CIA’s World Factbook links:
https://www.cia.gov/library/publications/resources/the-world-factbook/appendix/appendix-d.html
https://www.cia.gov/library/publications/the-world-factbook/attachments/flags/CM-flag.gif
https://www.cia.gov/library/publications/the-world-factbook/docs/flagsoftheworld.html
Note :
For those of you who have not yet updated to the latest Visio file format, here is the stencil in the old binary format: Image Utils.vss
Gustavo Garcia Lory (@garcialory) says
Great work David! It opens to Visio developers like me a new world of possibilities to manage photos in Visio based systems. Thumbs up!