Site icon bVisual

Using the CALLTHIS function in Visio

Visio was the first non-Microsoft application to include VBA within it back in the mid-nineties. All of the desktop Microsoft Office applications currently include VBA, although Microsoft have been rumoured to want to replace it for many years, and now there is an alternative scripting option becoming available that is suitable for the web too.

Visio was acquired by Microsoft in 2000, and there are still many scenarios when it is useful to have VBA procedures available in Visio for the Desktop. These procedures can be created using the recorder, and then edited, or just created manually. Every Visio document can contain a VBA project, but it must be saved with the .vsdm extension for documents, .vssm extension for stencils, or .vstm extension for templates. The last letter, m, informs Windows that this file could contain macros, as opposed to the normal last letter x, which cannot. Even with this extension, users, or their IT department, may not enable the macros in Visio documents.

The CALLTHIS function in Visio can be used to run a procedure (sub-function) in a Visio Visual Basic document and pass through a reference to the shape that the function is called from. The Microsoft Docs provide an explanation of this useful technique, and it does describe some examples, but it does not show the VBA code that these examples need, nor does it show them working. It also does not make it clear that these procedures do not need to be in the Visio diagram document, but can be in a stencil that is open in the active Visio session. This is important because it means that VBA code can be centralised and distributed for use use on many other documents.

The shapes on the Visio document call code in the diagram document itself (CallThisExamples.vsdm), or in the stencil (CallThisExamples.vssm). They should be downloaded an placed into the same folder in OneDriveforBusiness. Opening the diagram document in Visio for the Desktop should then open the stencil in the workplace too.

VBA Code

The VBA code is duplicated in the document and in the stencil in order to demonstrate the how the procedures could be called from each.

ThisDocument is actually a Class, not a Module, and so needs to be included in the argument that is passed to the CALLTHIS function.

In Document

ThisDocument

Option Explicit

Public Sub A(vsoShape As Visio.Shape)
    MsgBox vsoShape.Name, _
        vbInformation, "ThisDocument.A"
End Sub

Public Sub B(vsoShape As Visio.Shape, arg1 As String)
    MsgBox vsoShape.Name & " with arg1=" & arg1, _
        vbInformation, "ThisDocument.B"
End Sub

Public Sub C(vsoShape As Visio.Shape, arg1 As String, arg2 As String)
    MsgBox vsoShape.Name & " with arg1=" & arg1 & ", arg2=" & arg2, _
        vbInformation, "ThisDocument.C"
End Sub

ModuleInDocument

Option Explicit

Public Sub p(vsoShape As Visio.Shape, heightInCm As String)
    MsgBox vsoShape.Name & " is " & heightInCm & " high", _
        vbInformation, "ModuleInDocument.p"
End Sub

Public Sub q(vsoShape As Visio.Shape, heightInCm As Double, widthInIU As Double)
    MsgBox vsoShape.Name & " is " & CStr(heightInCm) & " high by " & CStr(widthInIU) & " wide", _
        vbInformation, "ModuleInDocument.q"
End Sub

In Stencil

ThisDocument

Option Explicit

Public Sub A(vsoShape As Visio.Shape)
    MsgBox vsoShape.Name, _
        vbInformation, "CallThisExamples.ThisDocument.A"
End Sub

Public Sub B(vsoShape As Visio.Shape, arg1 As String)
    MsgBox vsoShape.Name & " with arg1=" & arg1, _
        vbInformation, "CallThisExamples.ThisDocument.B"
End Sub

Public Sub C(vsoShape As Visio.Shape, arg1 As String, arg2 As String)
    MsgBox vsoShape.Name & " with arg1=" & arg1 & ", arg2=" & arg2, _
        vbInformation, "CallThisExamples.ThisDocument.C"
End Sub

ModuleInStencil

Option Explicit

Public Sub p(vsoShape As Visio.Shape, heightInCm As String)
    MsgBox vsoShape.Name & " is " & heightInCm & " high", _
        vbInformation, "CallThisExamples.ModuleInStencil.p"
End Sub

Public Sub q(vsoShape As Visio.Shape, heightInCm As Double, widthInIU As Double)
    MsgBox vsoShape.Name & " is " & CStr(heightInCm) & " high by " & CStr(widthInIU) & " wide", _
        vbInformation, "CallThisExamples.ModuleInStencil.q"
End Sub

Synchronizing Visio Shape Fill Color (or almost any cell) across pages

I was recently asked how the color of one shape can be changed and for other shapes to be automatically updated to the same color … even if they are on different pages! Well, it is possible with Microsoft Visio’s awesome ShapeSheet formulas. In fact, this capability is not limited to the FillForegnd cell ……

Positioning Visio Shape Text Block with a Control Handle

I was recently asked how a control handle can be added to a Visio shape so that it can be used to re-position the text block. Fortunately, it is extremely easy to setup, and requires just two formulas to be updated in the ShapeSheet. This is a great use of the SETATREF(…) function. (more…)

Understanding Segments of Visio Geometry

I recently had to revise my understanding of the POINTALONGPATH(…) function in Visio because I was getting a #REF! error in some cases. My particular scenario requires a line with a number of vertices that are initially all in a straight line but can be moved by dragging controls around that each vertex is bound…

Custom Color Themes in Visio?

I was recently looking into custom color themes for corporate branding in desktop Microsoft Visio and became re-aware how different Visio still is from the rest of the Microsoft Office applications. A Visio page or document does not need to have any theme applied, but the documents of the other Office applications always have a…

When is a Visio Callout not a Callout?

I have been a Visio user/developer since the mid-1990’s and seen the word “callout” used as part of the name of many master shapes in Visio. The images below show five ways that the term “callout” has been applied to the name of Visio master shapes. Generally, each evolution has been an advance on the…

Using Visio Color by Value on Connectors

Data Graphics in Visio Plan 2 and Visio Professional is great, but it only enables us to use them with 2D shapes in Visio, i.e. not on connectors. So, what if you want to change the line colour of the connectors between the 2D shapes because of the data flowing between them? Well, it is…

Exit mobile version