我正在尝试从Excel内部生成Visio。 我可以让形状出现,但自动连接方法会因“类型不匹配”错误而爆炸。 我也尝试了GlueTo方法也没有运气。
可能是因为shpObj不是真正的形状吗?我需要帮助确定如果是这种情况以及如何更好地存储形状,或者如果可能的话保留shapeID。
shpFrom只是第一个shpObj ... 以下是我将形状放入Visio的方法:
Set shpObj = AppVisio.ActiveWindow.Page.Drop(AppVisio.Documents.Item("Computers and Monitors.vss").Masters.Item("PC"), dXPos, yPos)
以下是我尝试连接它们的方法:
shpObj.AutoConnect shpFrom, visAutoConnectDirUp
看起来很直接,但我无法让它发挥作用。 以下是所有代码:
Sub VisioFromExcel()
Set AppVisio = CreateObject("visio.application")
AppVisio.Visible = True
AppVisio.Documents.AddEx "Basic Network Diagram.vst", visMSmetric, 0
ComputerStencil = AppVisio.Documents.AddEx("Computers and Monitors.vss", visOpenRO + visOpenDocked)
Connector = AppVisio.Documents.AddEx("Connectors.vss", visOpenRO + visOpenDocked)
AppVisio.Windows.ItemEx(1).Activate
dXPos = AppVisio.ActivePage.PageSheet.Cells("PageWidth") / 2
dYPos = AppVisio.ActivePage.PageSheet.Cells("PageHeight") / 2
yPos = 1
For x = 4 To 6
'For x = 4 To Worksheets("Inventory").Cells(Rows.Count, 1).End(xlUp).Row
Set shpObj = AppVisio.ActiveWindow.Page.Drop(AppVisio.Documents.Item("Computers and Monitors.vss").Masters.Item("PC"), dXPos, yPos)
If x = 4 Then
shpFrom = shpObj
Else
'shpObj.GlueTo shpFrom
'Set line1 = AppVisio.ActiveWindow.Page.Drop(AppVisio.ConnectorToolDataObject, 1, 2)
'line1.CellsU("BeginX").GlueTo shpFrom.CellsU("PinX")
'line1.CellsU("EndX").GlueTo shpObj.CellsU("PinX")
shpObj.AutoConnect shpFrom, visAutoConnectDirUp
End If
'Level testing for positioning?
'If Len(objName) > 0 Then
'End If
objName = Worksheets("Inventory").Cells(x, 1).Value
shpObj.Text = objName
yPos = yPos - 1.25
Next
'AppVisio.ActiveWindow.Page.CenterDrawing
AppVisio.ActiveWindow.Page.ResizeToFitContents
Set AppVisio = Nothing
End Sub
答案 0 :(得分:1)
在VBA中,在分配对象时,您应该使用" Set":
If x = 4 Then
Set shpFrom = shpObj ' Note Set !
Else
....