Reputation: 23
I want to be able to import an image as a shape in Visio using VBA. I've tried to insert an image using the following, but it doesn't work...
myShape.Import(imagePath)
However, myPage.Import(imagePath)
works fine and places an image in the center of the page, but I have no way of manipulating it as a shape. I've tried searching the web, but can't seem to find a way to do this using VBA.
Upvotes: 1
Views: 2599
Reputation: 2698
Just expanding on @tim and @mike comments here's a quick snippet that wraps the import part into a function
Sub TryAddImage()
Dim vPag As Visio.Page
Set vPag = ActivePage
Dim shpImg As Visio.Shape
Set shpImg = AddImageShape(vPag, "C:\MyImage.jpg")
If Not shpImg Is Nothing Then
'Do something to your new shape
shpImg.CellsU("Width").FormulaU = "=Height*0.5"
End If
End Sub
Private Function AddImageShape(ByRef vPag As Visio.Page, fileName As String) As Visio.Shape
Dim shpNew As Visio.Shape
If Not vPag Is Nothing Then
Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("Insert image shape")
On Error Resume Next:
Set shpNew = vPag.Import(fileName)
If Not shpNew Is Nothing Then
Application.EndUndoScope UndoScopeID1, True
Else
Application.EndUndoScope UndoScopeID1, False
End If
End If
Set AddImageShape = shpNew
End Function
Basically, the function tries to return a shape from the import method. If the method creates an error, either by a non-existent path or from the appropriate image filter not being installed, then the function returns 'Nothing', and you can then test for this in your calling code.
Upvotes: 1