AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Visual Lisp and VBA

In this tutorial we are going to do something a bit different. We're going to look at how we can use Visual Basic for Applications within our Visual Lisp code, especially in regards to dialog boxes. Firstly though, let's have a look at how we are going to go about loading and running the VBA application that we want to use. Copy and paste this into Notepad and save it as "Al-Eval.lsp" :

(defun c:al-eval ()

(vl-load-com)

(setq applic (vlax-get-acad-object))

(vla-eval applic (strcat "MsgBox \"Hello Everybody\"" ", "
 "vbInformation" ", " "\"AfraLisp\""))

(vla-eval applic (strcat "MsgBox \"Are You Fine?\"" ", "
"vbQuestion" ", " "\"CAD Encoding\""))

(vla-eval applic (strcat "MsgBox \"Or, are you not Well?\"" ", "
"vbCritical" ", " "\"VBA Expresso\""))

(alert "\nAnd this is the boring\nAutoCAD message box!!")

(princ)

);defun

Now load and run the routine. Four dialog boxes should appear, one after the other.

The first three dialogs, similar to the above, are standard VBA message boxes, and the fourth the AutoLisp alert box. By using the Visual Lisp "eval" method, we were able to "call" a standard VBA function from within Visual Lisp.


Let's get a bit more clever now, and try and pass information backwards and forwards between Visual Lisp and VBA.

Open the VBA Editor and create a new Project. Insert a UserForm and add a TextBox and two command buttons, retaining their default names. Your UserForm should look something like this :

Now add the following coding to the the Click events for the OK and Cancel buttons :

 Private Sub CommandButton1_Click()

ThisDrawing.SetVariable "USERS1", TextBox1.Text
End

End Sub
'--------------------------------------------------------
Private Sub CommandButton2_Click()
End
End Sub
 

Save you Project as "Testdial.dvb".

Now open Notepad and copy and paste the following :

(defun c:testdial ()

(vl-load-com)

(setq applic (vlax-get-acad-object))

(if (findfile "testdial.dvb")

   (progn

       (vl-vbaload "testdial.dvb")

       (vla-eval applic "userform1.show")

       (alert (strcat "You entered : " (getvar "users1")))

   );progn

       (alert "\nCannot find DVB file")

  );if

(princ)

);defun

(princ)

Load and run "Testdial.lsp". A dialog like this should appear :

Enter something into the TextBox and select "OK".
An alert box like this should appear:

This is what happened :

  • First, we loaded and called the VBA dialog.

  • Secondly, we entered the information.

  • Still in VBA, we stored the information into a User system variable.

  • We then closed the dialog.

  • Back in Visual Lisp, we retrieved the information from the User system variable and displayed it in an AutoLisp alert box.


On the next page, we'll have a look at a more practical use for this procedure.

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website