D
deko
I need to add a code module to Excel from Access via automation.
I've looked at these 2 sites for help:
http://www.j-walk.com/ss/excel/tips/tip96.htm
http://www.cpearson.com/excel/vbe.htm
For starters, I'm trying to simply add a module to an Excel workbook from a
standard Excel module.
The code below returns this:
This workbook has 6 modules.
Error Number 440: Method 'Add' of object '_VBComponents' failed
Is there something wrong with the way I'm instantiating newmod?
Any guidance would be appreciated.
Thanks in advance.
Public Function AddModuleToExcel()
On Error GoTo HandleErr
Dim intCt As Integer
Dim vbp As Object
Dim newmod As Object
If Val(Application.Version) >= 10 Then
Set vbp = ActiveWorkbook.VBProject
If Err.Number <> 0 Then
MsgBox "Your security settings do not allow this procedure to
run." _
& vbCrLf & vbCrLf & "To change your security setting:" _
& vbCrLf & vbCrLf & " 1. Select Tools - Macro - Security." &
vbCrLf _
& " 2. Click the 'Trusted Sources' tab" & vbCrLf _
& " 3. Place a checkmark next to 'Trust access to Visual Basic
Project.'", _
vbCritical
Exit Function
Else
intCt = vbp.VBComponents.Count
Debug.Print "This workbook has " & intCt & " modules."
Set newmod = vbp.VBComponents.Add(vbext_ct_StdModule)
newmod.Name = "MyNewModule"
intCt = vbp.VBComponents.Count
Debug.Print "This workbook has " & vbpCt & " modules."
End If
End If
Exit_Here:
Exit Function
HandleErr:
Debug.Print "Error Number " & Err.Number & ": "; Err.Description
End Function
I've looked at these 2 sites for help:
http://www.j-walk.com/ss/excel/tips/tip96.htm
http://www.cpearson.com/excel/vbe.htm
For starters, I'm trying to simply add a module to an Excel workbook from a
standard Excel module.
The code below returns this:
This workbook has 6 modules.
Error Number 440: Method 'Add' of object '_VBComponents' failed
Is there something wrong with the way I'm instantiating newmod?
Any guidance would be appreciated.
Thanks in advance.
Public Function AddModuleToExcel()
On Error GoTo HandleErr
Dim intCt As Integer
Dim vbp As Object
Dim newmod As Object
If Val(Application.Version) >= 10 Then
Set vbp = ActiveWorkbook.VBProject
If Err.Number <> 0 Then
MsgBox "Your security settings do not allow this procedure to
run." _
& vbCrLf & vbCrLf & "To change your security setting:" _
& vbCrLf & vbCrLf & " 1. Select Tools - Macro - Security." &
vbCrLf _
& " 2. Click the 'Trusted Sources' tab" & vbCrLf _
& " 3. Place a checkmark next to 'Trust access to Visual Basic
Project.'", _
vbCritical
Exit Function
Else
intCt = vbp.VBComponents.Count
Debug.Print "This workbook has " & intCt & " modules."
Set newmod = vbp.VBComponents.Add(vbext_ct_StdModule)
newmod.Name = "MyNewModule"
intCt = vbp.VBComponents.Count
Debug.Print "This workbook has " & vbpCt & " modules."
End If
End If
Exit_Here:
Exit Function
HandleErr:
Debug.Print "Error Number " & Err.Number & ": "; Err.Description
End Function