Shamil Salakhetdinov
shamil at users.mns.ru
Sat Jan 14 19:43:00 CST 2006
Correction - here is a working solution for both cases code running in FE
module and in add-in module:
Public Function a_test()
Dim objApp As Object
Set objApp = Access.Application ' for MS Access add-ins
'set objApp = <Application object reference COM Add-in gets on
' connection>
'
' Add reference to:
'
' 1. Microsoft Visual Basic for Applications
' Extensibility 5.3 - VBE6EXT.OLB
'
' before running this code
'
Dim strCode As String
Dim strName As String
strName = "CTest"
strCode = _
"public function Test()" & vbCrLf & _
" Msgbox ""Hello, World!""" & vbCrLf & _
"end function"
AddClassModule objApp, strName, strCode
End Function
Public Sub AddClassModule( _
ByRef rapp As Object, _
ByVal vstrName As String, _
ByVal vstrCode As String)
Dim prj As VBIDE.VBProject
Dim prjItem As VBIDE.VBComponent
Dim strLst As String
Set prj = rapp.VBE.VBProjects(rapp.GetOption("Project Name"))
Set prjItem = prj.VBComponents.Add(vbext_ct_ClassModule)
prjItem.CodeModule.AddFromString vstrCode
prjItem.Properties("Name").Value = vstrName
SendKeys "^s{Enter}"
End Sub
Shamil
----- Original Message -----
From: "Shamil Salakhetdinov" <shamil at users.mns.ru>
To: "Access Developers discussion and problem solving"
<accessd at databaseadvisors.com>
Sent: Saturday, January 14, 2006 8:08 PM
Subject: Re: [AccessD] Class builder
> John,
>
> This should work I think for both MS Access and COM-Add ins - please try:
>
> Public Function a_test()
> dim objApp as object
>
> set objApp = Access.Application ' for MS Access add-ins
> 'set objApp = <Application object reference COM Add-in gets on
> ' connection>
>
> '
> ' Add reference to:
> '
> ' 1. Microsoft Visual Basic for Applications
> ' Extensibility 5.3 - VBE6EXT.OLB
> '
> ' before running this code
> '
> Dim strCode As String
> Dim strName As String
> strName = "CTest"
> strCode = _
> "public function Test()" & vbCrLf & _
> " Msgbox ""Hello, World!""" & vbCrLf & _
> "end function"
> AddClassModule objApp, strName, strCode
> End Function
>
> Public Sub AddClassModule( _
> ByRef rapp As object, _
> ByVal vstrName As String, _
> ByVal vstrCode As String, _
> Optional ByVal vstrProjectName As String = "")
> Dim prj As VBIDE.VBProject
> Dim prjItem As VBIDE.VBComponent
> Dim strLst As String
>
> If Len(vstrProjectName) = 0 Then
> Set prj = rapp.VBE.ActiveVBProject
> Else
> Set prj = rapp.VBE.VBProjects.Item(vstrProjectName)
> End If
>
> Set prjItem = prj.VBComponents.Add(vbext_ct_ClassModule)
> prjItem.CodeModule.AddFromString vstrCode
> prjItem.Name = vstrName
> rapp.DoCmd.Save acModule, vstrName
> End Sub
>
> > apparently the .RunCommand acCmdNewObjectClassModule
> > isn't available in the
> > context of code running out in an add-in.
> .RunCommand acCmdNewObjectClassModule is available in an addin but you
have
> to have your front-end MS Access mdb/adp opened and of course code line
> should be:
>
> objApp.RunCommand ....
>
> where objApp is Access.Application object reference, which COM Add-in gets
> on connection and MS Access Add-in has natively as default instance of
> Access.Application ...
>
> Shamil
>
<<< tail skipped>>>