你们好来自stackoverflow的伙计们,
我目前正在使用Excel和Visio创建一个工具来收集有关我们的it基础设施的所有数据,以便我们的it部门对我们的it资产有一个大致的了解。使用excel收集所有数据,并用visio形状绘制我们的资产网络图,这是非常酷的。但有一件事对我们来说非常重要,在使用这个工具的情况下,我们希望它们之间的双向连接能够在excel中进行更改(并且visio可以识别),我们希望在visio中更改形状数据(并将其写到excel中)。在visio中,您可以通过单击“更新数据库中的记录”来手动更新excel文件,并通过在形状的上下文菜单中选择“更新形状-数据”来从excel加载数据。在shape的shape-sheet中,我有一行,例如User。用户定义单元格部分中的StartMakro_IP。在这个字段中,我有一个公式=CALLTHIS("update_item")+DEPENDSON(Prop.IP)。此公式调用update_item方法(位于模块“mdl_admin”中),以将更改记入我的excel文件:
Public Sub update_item(shp As Visio.Shape)
If shp.CellExists("User.ODBCConnection", Visio.visExistsAnywhere) <> 0 Then
'Loop thru to check if shape has refresh action
If shp.SectionExists(Visio.visSectionAction, Visio.visExistsAnywhere) <> 0 Then
For iRow = 0 To shp.RowCount(Visio.visSectionAction) - 1
If shp.CellsSRC(Visio.visSectionAction, iRow, Visio.visActionAction).Formula = "RUNADDON(""DBU"")" Then
'Force the cell formula to run
shp.CellsSRC(Visio.visSectionAction, iRow, Visio.visActionAction).Trigger
Exit For
End If
Next iRow
End If
End If
End Sub为了能够从excel中加载最新的更改,我们调用方法selectionchanged,每次选择一个形状时。此方法位于ThisDocument中,并按如下方式实现:
Dim WithEvents MyWindow As Visio.Window
Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub
Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
If Window.Selection.Count = 1 Then
If Window.Selection(1).CellExists("User.ODBCConnection", Visio.visExistsAnywhere) <> 0 Then
'Loop thru to check if shape has refresh action
If Window.Selection(1).SectionExists(Visio.visSectionAction, Visio.visExistsAnywhere) <> 0 Then
For iRow = 0 To Window.Selection(1).RowCount(Visio.visSectionAction) - 1
If Window.Selection(1).CellsSRC(Visio.visSectionAction, iRow, Visio.visActionAction).Formula = "RUNADDON(""DBR"")" Then
'Force the cell formula to run
Window.Selection(1).CellsSRC(Visio.visSectionAction, iRow, Visio.visActionAction).Trigger
'MsgBox Window.Selection(1).Name
Exit For
End If
Next iRow
End If
End If
End If
End Sub在我看来,问题是,当前实现的方法会触发自身(数据库更新/从数据库读取数据),因此将出现一个无尽的循环。实际上,Visio此时会崩溃,应用程序将不幸关闭。
有没有人知道另一种方法,可以将已更改的形状数据从当前visio-shape写入excel,同时从excel中读取新数据,并更新当前选定visio-shape的形状数据?
任何帮助都是非常感谢的。如果可能,请提供一些捕获异常的小示例代码,因为我不太熟悉visio中的事件。
诚挚的问候
约尔格
发布于 2015-02-23 17:08:31
来自这里的(visguy.com/vgforum/index.php?topic=6086.msg24731#msg24731)的BackSync插件解决了所有的问题,减少了很多痛苦!
https://stackoverflow.com/questions/27603856
复制相似问题