Search notes:

Examples for VBScript MS-Office App Creator: Call a callback function from Excel

This is one of the examples for the command line Office App Creator.
This example demonstrates how an instance of an object can be passed to the VBA that is running in Excel and how a method can be called on that instance.

run.wsf

run.wsf creates a call back object in order to receive message from Excel when the code is executed.
<job>
<script language="VBScript" src="..\..\..\create-MS-Office-app.vbs" />
<script language="VBScript">

   option explicit

   class callBack ' {
     '
     '   Define a simple class with one method to be called from
     '   the Excel application.
     '
     '
       sub message(txt) ' {
           wscript.echo("Excel says: " & txt)
       end sub ' }

   end class ' }


   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

   importVBAFile app, currentDir() & "functions.bas"

   dim cb
   set cb = new callBack
   app.run "main", cb

   xls.save

   wscript.echo "The end"

</script> </job>
Github repository VBS-MS-Office-App-Creator, path: /examples/Excel/callback/run.wsf

functions.bas

functions.bas contains the sub main which is called from run.wsf and gets the instance of callBack object that was created in run.wsf.
main calls message twice. The messages will be displayed in the console from where run.wsf` was executed.
option explicit

sub main(callback as variant) ' {

    callback.message("Main was started")

    cells(1,1) = "Hello world"

    callback.message("Finished.")

end sub ' }
Github repository VBS-MS-Office-App-Creator, path: /examples/Excel/callback/functions.bas

Index