Search code examples
vbavisio

Visio: Accessing Custom Color Variant from macro?


I created a custom color set in Visio 2016 but cannot access it from a macro. Even the recorded macro below errors out with:

Run-time error '-2032465751 (86db08a9)':


Invalid parameter.

The debugger highlights the line with 65535 in it. Any ideas how to pull this off using a macro? Thanks!

Sub Macro3()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    ActivePage.SetTheme 33, 33, 33, 33, 33

    ActivePage.SetTheme ActivePage.GetTheme(visThemeTypeIndex), 65535, ActivePage.GetTheme(visThemeTypeEffect), ActivePage.GetTheme(visThemeTypeConnector), ActivePage.GetTheme(visThemeTypeFont)

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

UPDATE Here's a working code example for others. Use PrintMasterGuids to find the GUID (the number in curly braces).

Public Sub PrintMasterGuids()
    '// Use this to find the master GUID for a custom color variant.
    Dim mst As Master
    Dim vDoc As Document
    'Change ThisDocument to the target if you're
    'not running the code from the same place
    Set vDoc = ThisDocument
    For Each mst In vDoc.Masters
        Debug.Print mst.NameU, mst.UniqueID
    Next
End Sub
Sub Theme_Office_with_Custom_colors()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Apply Theme to Document")

    Dim doc As Visio.Document
    Set doc = Visio.ActiveDocument

     '// Loop through pages:
    For Each pg In doc.Pages
        '// Office theme
        pg.SetTheme 33, 33, 33, 33, 33

        '// Apply the Custom colors
        Dim UndoScopeID2 As Long
        UndoScopeID2 = Application.BeginUndoScope("Apply Theme Variant")
        pg.PageSheet.CellsU("ColorSchemeIndex").FormulaU = "=USE({76B4C447-0406-0000-8E40-00608CF305B2})*0+65535"
        Application.EndUndoScope UndoScopeID2, True
    Next

    Application.EndUndoScope UndoScopeID1, True
    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

Solution

  • As far as I'm aware, you can't use a custom color set directly with the api, so you've got to dip down and address the relevant cell directly.

    When you create a custom color set Visio creates a separate (hidden) master to store the RGBs. This master is assigned a GUID and you have to use this inside a USE function in the ColorSchemeIndex cell.

    So in your code, if the only thing you want to change is the color cell then you could do this:

    ActivePage.PageSheet.CellsU("ColorSchemeIndex").FormulaU = "=USE({007C1AB0-0002-0000-8E40-00608CF305B2})*0+65535"
    

    ...where the GUID is a reference to your master.

    To get hold of the GUID in the first place, just apply your custom color set to the page and then inspect the ColorSchemeIndex cell formula, or, you could loop through the masters in your document and report the .UniqueID property as described here: http://visualsignals.typepad.co.uk/vislog/2013/10/customizing-themes-in-visio-2013.html