首页 > 解决方案 > MS Visio 使用 VBA 拖放自定义形状

问题描述

我似乎无法弄清楚如何使用 VBA 删除形状。

我想做的是:用户打开一个用户窗体并在文本框中输入一些东西。单击命令按钮时,我想从自定义模具(即shapes.vssx)加载形状(即资源),将用户条目写入ShapeData(即在Props.Name中写入名称字符串),然后将其放在某处工作表。我知道我必须使用 Shape.Drop 方法,但是如何引用我想用于创建新形状的特定主形状?

到目前为止,我正在尝试这个

Private Sub CommandButton1_Click()
   Dim shp As Visio.Shape
   Dim page As Visio.page
   Set page = Application.ActiveWindow.page
   Set shp = Application.Documents.Item("shapes.vssx").Masters.ItemU("ressource")

   page.Drop shp, 1, 1
End Sub

它返回类型不匹配。我错过了什么?

标签: vbams-officevisio

解决方案


您希望删除 aMaster而不是 a Shape,因此请尝试对您的代码进行此修改(未经测试):

Private Sub CommandButton1_Click()
   Dim mst as Visio.Master
   Dim shp As Visio.Shape
   Dim pag As Visio.page
   Set pag = Application.ActiveWindow.Page
   Set mst = Application.Documents.Item("shapes.vssx").Masters.ItemU("ressource")
   'You might also want to add some checks that the target document and then master exist
   Set shp = pag.Drop(mst, 1, 1)
End Sub

推荐阅读