I have an add-in that i have created with an add new module function. Here is the code:
Public Function CreateModule(strModulename As String)
Dim strModName As String
10 On Error GoTo err
20 DoCmd.RunCommand acCmdNewObjectModule
30 strModName = "Module1"
40 DoCmd.Save acModule, strModName
50 DoCmd.Rename strModulename, acModule, strModName
60 DoCmd.Save acModule, strModulename
ExitHere:
70 Exit Function
80 err: Call LogError("CreateModule-Utils")
End Function
When i run it gives an error "The command or action 'NewObjectModule' isn't available now.". However if i put a break and step through it gives no error and works properly have tried sleep and DoEvent to no avail. Any ideas appreciated.
type here
Try using the VBA Extensibility library:
Public Sub AddModule(ByVal ModuleName As String)
Dim vbp As VBIDE.VBProject
Dim vbc As VBIDE.VBComponent
Dim currentVbProject As String
currentVbProject = Application.GetOption("Project Name")
Set vbp = Application.VBE.VBProjects(currentVbProject)
Set vbc = vbp.VBComponents.Add(vbext_ct_StdModule)
vbc.Name = ModuleName
' Insert code.
vbc.CodeModule.InsertLines 4, "Public Sub HelloWorld" & vbCrLf & " MsgBox(""Hello, World!"") " & vbCrLf & "End Sub"
' Save module.
DoCmd.Save acModule, ModuleName
Set vbp = Nothing
Set vbc = Nothing
End Sub
Required reference:
Microsoft Visual Basic for Application Extensibility 5.3
Original code: Philipp Stiefel Codekabinett