简体   繁体   English

在Visio中将图像插入为形状-VBA

[英]Insert Image as a shape in Visio - VBA

I want to be able to import an image as a shape in Visio using VBA. 我希望能够使用VBA在Visio中将图像导入为形状。 I've tried to insert an image using the following, but it doesn't work... myShape.Import(imagePath) 我尝试使用以下方法插入图像,但不起作用... 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. 但是, myPage.Import(imagePath)可以正常工作并将图像放置在页面的中央,但是我无法将其作为形状进行操作。 I've tried searching the web, but can't seem to find a way to do this using VBA. 我曾尝试在网上搜索,但似乎找不到使用VBA进行此操作的方法。

Just expanding on @tim and @mike comments here's a quick snippet that wraps the import part into a function 只是在@tim和@mike注释上展开,这是将导入部分包装到函数中的快速片段

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 . 基本上,该函数尝试从import方法返回形状。 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. 如果该方法通过不存在的路径或未安装适当的图像过滤器而产生错误,则该函数返回“ Nothing”,然后可以在调用代码中对此进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM