Search notes:

Dynamically create a VBA module while VBA is executing

The following example demonstrates how a VBA sub can be dynamically created in using the the CodeModule object of the B Editor Object Model and then called via application.run().
#if 0 then

thisWorkbook.VBProject.references.addFromGuid _
        GUID   :="{0002E157-0000-0000-C000-000000000046}", _
        major  :=  5,                                      _
        minor  :=  3

#end if

sub main() ' {

    dim comps as vbComponents
    set comps = application.vbe.activeVBProject.vbComponents

    dim comp  as vbComponent
    set comp  = comps.add(1)

    dim mdl as codeModule
    set mdl = comp.codeModule

    mdl.insertLines  1, "sub xyz(nm as long, tx as string)"
    mdl.insertLines  2, "   msgBox tx & "", the number is "" & nm"
    mdl.insertLines  3, "end sub"

    application.run "xyz", 42, "hello world"

    comps.remove comp

end sub ' }
Github repository about-MS-Office-object-model, path: /VBE/CodeModule/create-VBA-module-while-executing.vb

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1758180153, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/Office/Visual-Basic-Editor/Object-Model/CodeModule/create-VBA-module-while-executing(64): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78