TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: VovKa on November 13, 2007, 12:01:02 PM

Title: vba project using activex
Post by: VovKa on November 13, 2007, 12:01:02 PM
Is it possible to create a vba project and the insert an empty module using activex?
Just to make the following function work.
Code: [Select]
(defun Test (/ AcadObj ActiveCodePaneObj CodeModuleObj Name VBEObj)
  (setq Name (getstring "\nEnter your name: " t))
  (setq AcadObj (vlax-get-acad-object))
  (setq VBEObj (vla-get-VBE AcadObj))
  (setq ActiveCodePaneObj (vlax-get-property VBEObj 'ActiveCodePane))
  (setq CodeModuleObj (vlax-get-property ActiveCodePaneObj 'CodeModule))
  (vlax-invoke-method
    CodeModuleObj
    'AddFromString
    (strcat "Sub Test()\nMsgBox \"" "Hello " Name "\"\nEnd Sub")
  )
  (vl-vbarun "Test")
  (foreach Obj (list AcadObj VBEObj ActiveCodePaneObj CodeModuleObj)
    (vlax-release-object Obj)
  )
  (gc)
)
Title: Re: vba project using activex
Post by: MP on November 13, 2007, 12:28:35 PM
Tip 1: Before you invoke AddFromString you must ensure you're not trying to establish a Function or Sub that already exists.

Tip 2: Use the Find function to determine it for you (over simplified in interests of brevity) --

Code: [Select]
(vlax-invoke
    codeModuleObj
   'Find
    "Sub Test"                             ;; What
    0                                      ;; StartLine
    0                                      ;; StartColumn
    (vlax-get codeModuleObj 'CountOfLines) ;; EndLine
    100                                    ;; EndColum
)

:)
Title: Re: vba project using activex
Post by: VovKa on November 13, 2007, 04:58:31 PM
thanks MP, but i need somthing to start with, i can not make vla-Add to add a new vba project. as soon as i find the way out, i will engage in error checking and so on...