首页 > 解决方案 > Set user-defined cell value in Visio document shape sheet from a macro.

问题描述

I added a row to the User-defined Cells section of the main Visio document shape sheet called User.Revision. I can use the value in the drawings by adding a field set to use a Custom Formula =TheDoc!User.Revision.

I would like to have a macro to set this Cell value but I can't find a way to reference the User-defined Cells in VBA. TheDoc!User.Revision doesn't work.

标签: vbavisio

解决方案


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

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

推荐阅读