Visio: VBA to change a shapes image, is it possible?

J

jeff_porter

Hello all,

I'd like to write some code to change (reload) all of the images used in
a visio document. In this case the artist has created new icons, and I
would like to 'automatically' reload the shapes in Visio.

I can tell which shapes to reload, I just don't know if its possible.

Can anyone help me out here? A nudge in the right direction would be great.

Thanks
Jeff Porter
 
J

John Goldsmith

Hello Jeff,

An image can't be swapped as such, or least not within it's original shape.
You have to replace the shape with a new one and here's so simple code that
shows how to work with the selected shape on a page:

Sub SwapImage()
Dim shpOriginal As Shape
Dim shpNew As Shape
Dim pag As Page
Dim sNewImage As String

sNewImage = "C:\MyNewImage.JPG"
Set shpOriginal = ActiveWindow.Selection.PrimaryItem

If Not shpOriginal Is Nothing Then
Set pag = shpOriginal.Parent
'Import new image
Set shpNew = pag.Import(sNewImage)
'Set new image's position
shpNew.CellsU("PinX").FormulaU = _
shpOriginal.CellsU("PinX").ResultIU
shpNew.CellsU("PinY").FormulaU = _
shpOriginal.CellsU("PinY").ResultIU
'Now delete the old image
shpOriginal.Delete
End If

End Sub

If you have a more complex group shape you could swap the image shape within
it, or toggle the visibility between a number of sub-image shapes (nb you'd
have to modify the above code accordingly).

Hope that helps.

Best regards

John


John Goldsmith
www.visualSignals.co.uk
 
J

jeff_porter

John said:
Hello Jeff,

An image can't be swapped as such, or least not within it's original shape.
You have to replace the shape with a new one and here's so simple code that
shows how to work with the selected shape on a page:

Sub SwapImage()
Dim shpOriginal As Shape
Dim shpNew As Shape
Dim pag As Page
Dim sNewImage As String

sNewImage = "C:\MyNewImage.JPG"
Set shpOriginal = ActiveWindow.Selection.PrimaryItem

If Not shpOriginal Is Nothing Then
Set pag = shpOriginal.Parent
'Import new image
Set shpNew = pag.Import(sNewImage)
'Set new image's position
shpNew.CellsU("PinX").FormulaU = _
shpOriginal.CellsU("PinX").ResultIU
shpNew.CellsU("PinY").FormulaU = _
shpOriginal.CellsU("PinY").ResultIU
'Now delete the old image
shpOriginal.Delete
End If

End Sub

If you have a more complex group shape you could swap the image shape within
it, or toggle the visibility between a number of sub-image shapes (nb you'd
have to modify the above code accordingly).

Hope that helps.

Best regards

John


John Goldsmith
www.visualSignals.co.uk

Thanks John.

That might be an option, will have to see how complex the visio
documents get.

Shame the'res not a way to change the image. Oh well.

Thanks again.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top