通过宏在Visio文档形状表中设置用户定义的单元格值。

时间:2018-08-03 04:10:30

标签: vba visio

我在Visio主文档形状表的User-Revision的User-defined Cells部分添加了一行。我可以在图形中使用该值,方法是添加一个字段集以使用自定义公式= TheDoc!User.Revision。

我想要一个宏来设置此Cell值,但是我找不到在VBA中引用用户定义的Cell的方法。 TheDoc!User.Revision不起作用。

2 个答案:

答案 0 :(得分:0)

恕我直言,您可以在没有宏的情况下做出解决方案
希望这部影片能为您带来帮助-Global Shape Data in Visio Document

答案 1 :(得分:0)

所以这有点晚了,但是我想有人也许仍然可以使用它。

文档的ShapeSheet被称为DocumentSheet,可以这样访问:

Sub testReadWriteDocumentSheet()

    Dim someDoc As Visio.Document
    Set someDoc = ThisDocument 'or whatever other document you need

    Dim rowName As String 'the Name of the Row
    rowName = "ExampleRevision"

    Dim value As String 'the value you want to store, change to whatever format you need.
    value = "132ABC"

    'to set up the cell:
    PrepareCellOnDocumentSheet someDoc, rowName

    'to write
    someDoc.DocumentSheet.CellsU("User." & rowName).FormulaForceU = Chr$(34) & value & Chr$(34)
    'Chr$(34) is needed to add the quotes if you store a string,
    'if you store a Double or Long leave them out.

    'to read:
    Dim returnValue As String
    returnValue = someDoc.DocumentSheet.CellsU("User." & rowName).ResultStr(visNoCast)
    'returns the string but without the quotes
    Debug.Print returnValue

End Sub

奖金: 该代码会自动检查您要求的行(和用户部分)是否存在,如果不存在,则会添加它们。

Private Function PrepareCellOnDocumentSheet(ByVal someDoc as Visio.Document, ByVal rowName As String)
    With someDoc.DocumentSheet
        If Not .SectionExists(visSectionUser, False) Then
            .AddSection visSectionUser
        End If
        If Not .CellExistsU("User." & rowName, True) Then
            .AddNamedRow visSectionUser, rowName, visTagDefault
        End If
    End With
End Function