• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Visio Guy

Smart graphics for visual people




  • Home
  • Hire Me
    • Hire Me
    • Résumé
  • Products
    • Products
    • Bubble Revision Shape
    • Layers to Pages Utility
    • Rack Unit Dimension Line
    • Radial Elements Tool with up to 100 Wedges
    • Text on a Circle Visio SmartShape
  • Index
    • Articles by Date
    • YouTube – VisioGuy
    • Download Information
    • Suggestion Box
    • Shop
    • Visio Art
    • Visio Links
    • – Visio Shapes & Stencils
    • – Visio Templates & Drawings
  • About
    • About
    • Donate
    • GitHub
    • jsFiddle
    • Reading List
    • Subscribe & Follow
      • – E-mail
      • – facebook
      • – Twitter
      • – RSS
    • Privacy Policy
  • Discussion Forum
You are here: Home / Add-ins & Tools / VBA Class Property Generator

VBA Class Property Generator

May 20, 2009 By Visio Guy 7 Comments

vba-class-properties-generatorDon’t you hate typing Property Get and Property Let and Property Set into your VBA class modules over and over and over and over?

Today I’d like to share a little VBA code module that I like to use to generate VBA class properties.

In .NET, Visual Basic’s syntax has gotten more efficient which makes the job easier. Visual Studio has gotten smarter: it assists you with a lot of mundane typing tasks. So once you get used to modern technology, going back to VBA or VB6 can be rather painful.

So I’ve created this utility code to ease my sorrow, and hopefully help you out too!

A lot of customers still like to get smaller solutions built in VBA. I do a lot of automation work with Visio, and customers like that they can get in and tweak the code without having to do a re-compile. At very worst, they can sick a summer intern on it. There’s a comfort in knowing that the consultant’s work isn’t buried in some .exe or .dll that nobody will know how to update. Fair enough.

When you create a class, you often times declare a private variable, then expose it with public properties. Your private variable might have some sort of prefix to distinguish it from the public variables, and an initial lower-case letter:

1
Private m_visShape As Visio.Shape
Private m_visShape As Visio.Shape

Then you expose them with public properties. They usually don’t have the prefixes, and have initial caps, like these:

1
2
3
4
5
6
7
Public Property Get VisShape() As Visio.Shape
  Set VisShape = m_visShape
End Property
 
Public Property Set VisShape(value as Visio.Shape)
  Set m_visShape = value
End Property
Public Property Get VisShape() As Visio.Shape
  Set VisShape = m_visShape
End Property

Public Property Set VisShape(value as Visio.Shape)
  Set m_visShape = value
End Property

So you have to type Visio.Shape three times, Set twice, Property twice (VBA does the End Property for you), and VisShape + m_visShape six times! This is mind-numbing at best, especially when you consider that the code hardly does anything other than “enforce good coding habits.”

I figured, I would rather just type the variable names and data types once, then let some code automate the creation of the properties. The VBA module that accompanies this article does just that.

First you configure a string-constant with a list of names and types, separated by semicolons:

1
2
3
Private Const PropertyList$ = _
  "ID;String;VisShape;Visio.Shape;VisDoc;Visio.Document;" & _
  "VisPage;Visio.Page;ShapeID;Integer"
Private Const PropertyList$ = _
  "ID;String;VisShape;Visio.Shape;VisDoc;Visio.Document;" & _
  "VisPage;Visio.Page;ShapeID;Integer"

Then you run the code, and it creates the skeleton of a VBA class for you in the Debug (or Immediate) window! You can just copy and paste the output into a new class definition.

The code-generation does a few other things as well:

  • Adds “Option Explicit” at the beginning
  • Determines which data types are objects that require Set instead of Let
  • Creates some header comments
  • Creates a ToString() function that serves as a text dump for you class.
    Be sure and read the comments that are written in this function!
  • Creates a Class_Terminate function that sets all object-variables to Nothing

When you run the code for the PropertyList$ constant shown above, you’ll get this as a result:

Note: this is the output code, not the source code!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'// Class: C..._
'// A class for...
 
Private m_iD As String
Private m_visShape As Visio.Shape
Private m_visDoc As Visio.Document
Private m_visPage As Visio.Page
Private m_shapeID As Integer
 
Public Property Get ID() As String
    ID = m_iD
End Property
Public Property Let ID(value As String)
    m_iD = value
End Property
 
Public Property Get VisShape() As Visio.Shape
    Set VisShape = m_visShape
End Property
Public Property Set VisShape(value As Visio.Shape)
    Set m_visShape = value
End Property
 
Public Property Get VisDoc() As Visio.Document
    Set VisDoc = m_visDoc
End Property
Public Property Set VisDoc(value As Visio.Document)
    Set m_visDoc = value
End Property
 
Public Property Get VisPage() As Visio.Page
    Set VisPage = m_visPage
End Property
Public Property Set VisPage(value As Visio.Page)
    Set m_visPage = value
End Property
 
Public Property Get ShapeID() As Integer
    ShapeID = m_shapeID
End Property
Public Property Let ShapeID(value As Integer)
    m_shapeID = value
End Property
 
Public Function ToString() As String
 
    '// Note: not all of the variables in this class will
    '//       necessarily be convertible to strings. In these
    '//       cases, you will get errors in this function.
    '//       In these cases, either remove the line that contains
    '//       a variable that can't be converted to string, or
    '//       substitute some option that can be, such as
    '//       m_someObject.String or m_someObject.Name
 
    Dim s As String
 
    s = "ID = " & CStr(m_iD) & vbCrLf &
        "VisShape = " & CStr(m_visShape) & vbCrLf &
        "VisDoc = " & CStr(m_visDoc) & vbCrLf &
        "VisPage = " & CStr(m_visPage) & vbCrLf &
        "ShapeID = " & CStr(m_shapeID)
 
    ToString = s
 
End Function
 
Private Sub Class_Terminate()
 
    Set m_visShape = Nothing '//...(Type = Visio.Shape)
    Set m_visDoc = Nothing '//...(Type = Visio.Document)
    Set m_visPage = Nothing '//...(Type = Visio.Page)
 
End Sub
'// Class: C..._
'// A class for...

Private m_iD As String
Private m_visShape As Visio.Shape
Private m_visDoc As Visio.Document
Private m_visPage As Visio.Page
Private m_shapeID As Integer

Public Property Get ID() As String
    ID = m_iD
End Property
Public Property Let ID(value As String)
    m_iD = value
End Property

Public Property Get VisShape() As Visio.Shape
    Set VisShape = m_visShape
End Property
Public Property Set VisShape(value As Visio.Shape)
    Set m_visShape = value
End Property

Public Property Get VisDoc() As Visio.Document
    Set VisDoc = m_visDoc
End Property
Public Property Set VisDoc(value As Visio.Document)
    Set m_visDoc = value
End Property

Public Property Get VisPage() As Visio.Page
    Set VisPage = m_visPage
End Property
Public Property Set VisPage(value As Visio.Page)
    Set m_visPage = value
End Property

Public Property Get ShapeID() As Integer
    ShapeID = m_shapeID
End Property
Public Property Let ShapeID(value As Integer)
    m_shapeID = value
End Property

Public Function ToString() As String

    '// Note: not all of the variables in this class will
    '//       necessarily be convertible to strings. In these
    '//       cases, you will get errors in this function.
    '//       In these cases, either remove the line that contains
    '//       a variable that can't be converted to string, or
    '//       substitute some option that can be, such as
    '//       m_someObject.String or m_someObject.Name

    Dim s As String

    s = "ID = " & CStr(m_iD) & vbCrLf &
        "VisShape = " & CStr(m_visShape) & vbCrLf &
        "VisDoc = " & CStr(m_visDoc) & vbCrLf &
        "VisPage = " & CStr(m_visPage) & vbCrLf &
        "ShapeID = " & CStr(m_shapeID)

    ToString = s

End Function

Private Sub Class_Terminate()

    Set m_visShape = Nothing '//...(Type = Visio.Shape)
    Set m_visDoc = Nothing '//...(Type = Visio.Document)
    Set m_visPage = Nothing '//...(Type = Visio.Page)

End Sub

Here’s the VBA module that will help you get it done:

Download “VBA Property Generator Code Module”

s!Aj0wJuswNyXlhxl5S4JeVGcp6-9k – Downloaded 2357 times – 103.00 B
  • Tweet
  • More
  • Pocket
  • Share on Tumblr
  • Print
  • Email

Related posts:

  1. Circular Text Generator (version 2)

Filed Under: Add-ins & Tools, Code, Tools

Previous Post: « Interview With a Visio Guy
Next Post: How to Count the Number of Items in a List »

Reader Interactions

Comments

  1. nashwaan says

    July 4, 2010 at 6:04 am

    Hi Visio Guy,

    I have download the VBA but i don’t know how to install it. Should i copy the .bas file to certain folder? and how to run this VBA module.

    Thanks,
    Yousuf

  2. Visio Guy says

    July 4, 2010 at 4:26 pm

    Hi Yousuf,

    Usually you can import .BAS files into a VBA project.

    To use the code, do as the article says: “First you configure a string-constant with a list of names and types, separated by semicolons”

    This is the PropertyList$ near the beginning of the code.

    Then you simply run Sub VbaPropertyGenerator(). Your class will be output to the immediate (Debug) window.

  3. Chabu says

    April 17, 2011 at 10:06 am

    Hello Visio Guy,

    Thanks for the utility, It will save time (also by avoiding dumb typing errors)
    I Think you should add Date to the NativeTypes though.

    Greetings
    Bruno

  4. rikkouri says

    June 15, 2012 at 9:24 pm

    awesome tool! sick of writing properties! Thanks for this!

  5. semerkhet says

    August 18, 2014 at 6:31 pm

    very nice tool.
    bravo!

  6. chaz6 says

    April 27, 2016 at 12:54 pm

    Thanks for this! Please consider adding Date to the list of native types.

  7. Mike says

    February 7, 2019 at 9:42 pm

    I followed directions, was able to import to access. However, when I invoke this procedure I keep getting run-time error 40192 (application-defines or object-defined error). Looking into intermediate window I see where the tostring is failing around property in middle of list of defined properties. Any suggestions appreciated.

Leave a Reply Cancel reply

Primary Sidebar

Buy Text on a Circle Shape
Article — Video — Purchase

Categories

Buy my book!

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tag Cloud

A/V Artistic Effects BPM Code Connectors Control Handles Countries Custom Patterns Custom Properties Data Graphics Data Linking Data Visualization David Edson David Parker Fill Format Formulas Functions Geometry Gradient Images Links Maps Multi-shapes Network Programming repeating shapes Resources Right-Click Actions Scale Shape Data ShapeSheet ShapeSheet Formulas ShapeSheet Functions SharePoint shiny SmartShapes Sport Sports Text Themes Tools Transparency User-defined Cells Visio 2007 Visio SmartShapes

Top Posts & Pages

  • - Visio Shapes & Stencils
  • - Visio Templates & Drawings
  • Amazon AWS Visio Shapes
  • Sankey Diagram Shapes for Visio
  • Free Visio People Shapes
  • Bubble Revision Shapes
  • Visio Network Server Shape Icon Customization Tool
  • AV Engineering Diagrams with Symbol Logic ECAV
  • Crayon Visio Network Shapes, Revisited
  • Release the Power of Visio Custom Line Patterns

www.visguy.com - Visio Guy - since 2006

 

Loading Comments...