Her var det. Det er taget fra Erlandsens hjemmeside på
http://edc.bizhosting.com/english/Insert a new module from a file
With the macro below you can easily insert new modules with contents in a workbook. This requires that you have created the new module previously in another (temporary) workbook. Export the finished module to a text file by rightclicking the module name and select Export file... in the shortcut menu.
Sub InsertVBComponent(ByVal wb As Workbook, ByVal CompFileName As String)
\' inserts the contents of CompFileName as a new component in wb
\' CompFileName must be a valid VBA component suited for import (an exported VBA component)
If Dir(CompFileName) <> \"\" Then \' source file exist
On Error Resume Next \' ignores any errors if the project is protected
wb.VBProject.VBComponents.Import CompFileName \' inserts component from file
On Error GoTo 0
End If
Set wb = Nothing
End Sub
Example:
InsertVBComponent ActiveWorkbook, \"C:\\FolderName\\Filename.bas\"
Og hvis det er nødvendigt først at lave et nyt modul skal denne bruges:
Create a new module
With the macro below you can create a new module in a workbook:
Sub CreateNewModule(ByVal wb As Workbook, ByVal ModuleTypeIndex As Integer, ByVal NewModuleName As String)
\' creates a new module of ModuleTypeIndex (1=standard module, 2=userform, 3=class module) in wb
\' renames the new module to NewModuleName (if possible)
Dim VBC As VBComponent, mti As Integer
Set VBC = Nothing
mti = 0
Select Case ModuleTypeIndex
Case 1: mti = vbext_ct_StdModule \' standard module
Case 2: mti = vbext_ct_MSForm \' userform
Case 3: mti = vbext_ct_ClassModule \' class module
End Select
If mti <> 0 Then
On Error Resume Next
Set VBC = wb.VBProject.VBComponents.Add(mti)
If Not VBC Is Nothing Then
If NewModuleName <> \"\" Then
VBC.Name = NewModuleName
End If
End If
On Error GoTo 0
Set VBC = Nothing
End If
End Sub
Example:
CreateNewModule ActiveWorkbook, 1, \"TestModule\"
Utrolig, at der skal så meget til, men det er åbenbart nødvendigt.