Search notes:

VBA: attribute vb_predeclaredId

This is an example that tries to demonstrate the effect of the vb_predeclaredId attribute.
A (class) module that sets the value of vb_predeclaredId to true creates a global («predeclared») instance of that class without having to initialize this global instance with the new operator.
Besides this global instance, it is possible to create additional instances of this class.

predeclared.cls

This is the source code of the class whose attribute vb_predeclaredId is set to true:
attribute vb_predeclaredId   = true

option explicit

private member as long

sub class_initialize()
    debug.print "An instance of predeclared is being initialized"
end sub

sub class_terminate()
    debug.print "terminate predeclared instance, member = " & member
end sub

sub setMemberValue(m as long) ' {
    debug.print "setting member value to " & m
    member = m
end sub ' }

function getMemberValue() as long' {
    getMemberValue = member
end function ' }
Github repository about-VBA, path: /attribute/vb_predeclaredId/predeclared.cls

func.vb

This module accesses the global instance and creates an additional one, named abc:
option explicit

sub main() ' {

    predeclared.setMemberValue 42

    dim abc as new predeclared
    abc.setMemberValue 99

    debug.print "member value of predeclared is: " & predeclared.getMemberValue
    debug.print "member value of abc is:         " &         abc.getMemberValue

end sub ' }
Github repository about-VBA, path: /attribute/vb_predeclaredId/func.vb

Creating the project

The following VBScript file uses the VBScript MS-Office App Creator to create an Excel project in order to run main (in func.vb):
<job>
<script language="VBScript" src="../../VBS-MS-Office-App-Creator/create-MS-Office-app.vbs" />
<script language="VBScript">

   option explicit

   dim app
   dim xls
   set xls = createOfficeApp("excel", currentDir() & "created.xlsm")

   if xls is nothing then ' {
      wscript.echo("Could not create excel worksheet.")
      wscript.quit(-1)
   end if ' }

   set app = xls.application

   insertModule app, currentDir() & "func.vb"        , "func"       , 1
   insertModule app, currentDir() & "predeclared.cls", "predeclared", 2
 '
 ' Show VB editor
 '
   app.VBE.mainWindow.visible = true

   xls.save
   app.run "main"

   createObject("WScript.Shell").appActivate(app.caption)

</script></job>
Github repository about-VBA, path: /attribute/vb_predeclaredId/create.wsf
On the command line, it must be invoked with
P:\ath\to\project> cscript .\create.wsf

See also

A class that has the vb_predeclaredId attribute set to true seems to correspond to a COM Object whose TYPEFLAGS (enum) value is TYPEFLAG_FPREDECLID.

Index