2013-05-09 58 views
1

我希望能够使用VBA将图像作为Visio中的形状导入。我试着用下面的插入图像,但它不工作... myShape.Import(imagePath)将图像作为Visio中的形状插入 - VBA

然而,myPage.Import(imagePath)工作正常,并在页面的中心放置的图像,但我没有操纵方式它作为一个形状。我试过在网上搜索,但似乎无法找到使用VBA的方法。

+0

'Import()'返回对导入形状的引用。您可以使用该参考来操纵图像。 '设置shp =导入(imagePath)' – 2013-05-09 16:43:50

+0

喜蒂姆,虽然没有导入功能。 – 2013-05-09 17:07:13

+0

为了澄清,Page.Import返回一个您可以操作的形状。例如,其中一种操作可以是将新图像形状添加到作为图像周围框架的组中。 – 2013-05-09 17:33:42

回答

1

上@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 

基本上,函数试图从import method返回的形状。如果方法通过不存在的路径或未安装相应的图像过滤器创建错误,则该函数返回“Nothing”,然后可以在调用代码中对此进行测试。

+0

感谢您的帮助! – 2013-05-10 17:26:51

相关问题