I have been trawling the net looking for procedures to write code and modules to workbooks programatically and this is what i have found i cant get it to run even though i have set the reference to Microsoft Extensibility Library 5.3, it doesnt seem to like any reference to VBProject!. I thought this could have been used to write to all 27 workbooks either writing the reference you mentioned above or the entire code!....................me being lazy again!
Regards,
Simon
[VBA]
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As Long
Public Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long
Sub AddModule()
Application.VBE.MainWindow.Visible = False'''It doesnt like this line
Dim VBComp As VBComponent
Set VBComp = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "AddInRef"
Application.Visible = True
Call AddProcedure
End Sub

Sub AddProcedure()
Dim VBCodeMod As CodeModule
Dim LineNum As Long
Dim VBEHwnd As Long
On Error GoTo ErrH:
Application.VBE.MainWindow.Visible = False
VBEHwnd = FindWindow("wndclass_desked_gsk", _
Application.VBE.MainWindow.Caption)
If VBEHwnd Then
LockWindowUpdate VBEHwnd
End If
'
' your code to write code
'
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("AddInRef").CodeModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub MyNewProcedure()" & Chr(13) & _
" Msgbox ""Here is the new procedure"" " & Chr(13) & _
"End Sub"
End With
Application.Run "MyNewProcedure"
Application.VBE.MainWindow.Visible = True
'Application.VBE.MainWindow.Visible = False
Call WBO
ErrH:
LockWindowUpdate 0&
End Sub
Sub WBO()
Dim StartLine As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
StartLine = .CreateEventProc("Open", "Workbook") + 1
.InsertLines StartLine, "Msgbox ""Hello World"",vbOkOnly"
End With
End Sub
Function ProcedureExists(ProcedureName As String, _
ModuleName As String) As Boolean
Dim ModuleExists
On Error Resume Next
If ModuleExists(ModuleName) = True Then
ProcedureExists = ThisWorkbook.VBProject.VBComponents(ModuleName) _
.CodeModule.ProcStartLine(ProcedureName, vbext_pk_Proc) <> 0
End If
End Function

[/VBA]