回望南山
记忆痕迹可以鲜明, 回望往事如数家珍——
posts - 177,  comments - 54,  trackbacks - 0

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.

posted on 2008-04-05 16:36 深藏记忆 阅读(567) 评论(1)  编辑  收藏 所属分类: 转载Vlisp

FeedBack:
# re: Visual Lisp and VBA -- Page I
2010-01-31 17:25 | zml84
好文,学习了。。  回复  更多评论
  

飘过是缘,相识最真

订阅到抓虾
google reader
gougou


点击这里给我发消息


<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(5)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

八面来息

天天充电

同行者

积分与排名

  • 积分 - 58699
  • 排名 - 61

最新评论

阅读排行榜

评论排行榜