本帖最后由 自贡黄明儒 于 2024-7-26 13:36 编辑
自动新建模块module,并写入代码:
搜了大半天,也问了好多人。最后还是白度的AI帮我写的。为记念这一重要时刻,贴出来。。。
VB如下
- Sub CreateModule()
- Dim vbProj As Object
- Dim vbComp As Object
- ' 设置对当前工程的引用
- Set vbProj = Application.VBE.ActiveVBProject
-
- ' 添加一个新的module
- Set vbComp = vbProj.VBComponents.Add(1)
-
- ' 设置module的名称
- 'vbComp.Name = "MyNewModule"
-
- ' 现在可以在module中添加代码了
- vbComp.CodeModule.AddFromString "Sub MySub()" & vbCrLf & _
- " MsgBox ""Hello, World!""" & vbCrLf & _
- "End Sub"
-
- ' 可选:激活module编辑器
- Application.VBE.ActiveCodePane.CodeModule.InsertLines 1, "'"
- Application.VBE.ActiveCodePane.CodeModule.InsertLines 2, "'" & vbTab & "Created by CreateModule macro"
- Application.VBE.ActiveCodePane.CodeModule.InsertLines 3, "'"
- Application.VBE.ActiveCodePane.CodeModule.InsertLines 4, ""
-
- ' 提示用户module已创建
- MsgBox "Module created successfully!"
- End Sub
在lisp如下
- (defun c:Example_VBE (/ ACTIVEVBPROJECT CODEMODULE MODULE NEWROUTINE VBCOMPONENTS VBE)
- (setq VBE (vla-get-VBE (vlax-get-acad-object))) ;ActiveVBProject
- (setq ActiveVBProject (vlax-get-property VBE 'ActiveVBProject))
-
- (setq VBComponents (vlax-get-property ActiveVBProject 'VBComponents))
- (setq module (vlax-invoke VBComponents 'Add 1))
- (setq newRoutine
- (strcat "Sub Dynamic_Procedure()"
- "\n MsgBox \"New subroutine1.\""
- "\nEnd Sub"
- )
- )
- (setq CodeModule (vlax-get-property module 'CodeModule))
- (vlax-invoke CodeModule 'AddFromString newRoutine)
- )
|