【发布时间】:2020-06-30 21:20:32
【问题描述】:
我在 Visio 绘图中有一些形状,其中包含作为形状数据的列表。所有数据都在数据集中定义,我将其应用于所有模板。有时,我需要添加更多模具并更新数据集(在列表中插入新项目)。因为每次更新列表时,Visio 都会删除当前列表并创建一个新列表,因此我会丢失使用该数据集的所有形状的数据。
为了解决这个问题,我编写了一些 VBA 代码,用于创建临时存储并保存与每个形状对应的列表项,同时更新我的数据集。
以下是我写的。
Sub AddTemp()
Dim vPage As Visio.Page
Dim vShape As Shape
Dim vRowInt As Integer
Dim vCell As Cell
Dim MyList As Variant
Dim vValue As String
Dim vLabel As String
'Shape Data defined as Fixed/Variable List:
MyList = Array("List1", "List2")
'Loop through each page of the document
For Each vPage In ThisDocument.Pages
'Loop through each shape of each page of the document
For Each vShape In vPage.Shapes
'If ShapeData exists, do your thing
If vShape.SectionExists(visSectionProp, 0) Then
'Iterate through each element of the list
For Each element In MyList
'If Temp container does not exist, make one
If Not vShape.CellExistsU("Prop." + element + "Temp", 1) Then
vRowInt = vShape.AddRow(visSectionProp, visRowLast, visTagDefault)
vShape.Section(visSectionProp).Row(vRowInt).NameU = element + "Temp"
vLabel = "=" + element + "Temp"
'MsgBox vLabel
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = "=vLabel"
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsType).FormulaU = 0
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsFormat).FormulaU = ""
If vShape.CellExistsU("Prop." + element, 1) Then
vValue = "=Prop." + element + ".Value"
'MsgBox Value
Set vCell = vShape.CellsU("Prop." + element + "Temp.Value")
vCell.FormulaU = vValue
End If
End If
Next
End If
Next
Next
MsgBox "Temporary Storage Created"
End Sub
我目前遇到的问题是以下语句:
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = vLabel
我想根据 MyList 中的元素设置我创建的行的标签列,但无论我尝试使用什么,它似乎都不起作用:
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = element
或
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = `element`
或
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = "element"
或
vShape.CellsSRC(visSectionProp, vRowInt, visCustPropsLabel).FormulaU = "=element"
等等
不过,下面的代码可以正常工作:
vValue = "=Prop." + element + ".Value"
vCell.FormulaU = vValue
我本来希望 FormulaU 接受 MyList 数组的字符串元素,但我得到了
运行时错误“-2032466907 (86db0425)”: #姓名?
如何使用数组的元素来设置我添加的每一行的标签?
【问题讨论】: