【问题标题】:Insert Image as a shape in Visio - VBA在 Visio 中将图像作为形状插入 - VBA
【发布时间】:2013-05-09 16:37:58
【问题描述】:

我希望能够使用 VBA 在 Visio 中将图像作为形状导入。我尝试使用以下方法插入图像,但它不起作用... myShape.Import(imagePath)

但是,myPage.Import(imagePath) 可以正常工作并将图像放置在页面的中心,但我无法将其作为形状进行操作。我试过在网上搜索,但似乎找不到使用 VBA 的方法。

【问题讨论】:

  • Import() 返回对导入形状的引用。您可以使用该引用来操作图像。 Set shp = Import(imagePath)
  • 嗨蒂姆,虽然没有导入功能。
  • 澄清一下,Page.Import 返回一个您可以操作的形状。例如,其中一种操作可以是将新的图像形状添加到充当图像周围框架的组中。
  • @S. Multani - 文档说该方法的返回值为“Shape”。你试过了吗?顺便说一句,没有严格的定义说方法不能返回值。函数也是方法。
  • 是的,感谢您解决这个问题。我对Import(imagePath) 感到困惑,因为它看起来好像在 Visio 库中有一个内置函数 Import。

标签: vba visio


【解决方案1】:

只是扩展 @tim 和 @mike cmets 这是一个快速的 sn-p,它将导入部分包装到一个函数中

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

基本上,该函数尝试从import method 返回一个形状。如果该方法由于不存在的路径或未安装适当的图像过滤器而产生错误,则该函数将返回“Nothing”,然后您可以在调用代码中对此进行测试。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多