LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default How to add code module to Excel programmatically

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_He
Exit Function
HandleErr:
Debug.Print "Error Number " & Err.Number & ": "; Err.Description
End Function


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Excel Discussion (Misc queries) 4 December 11th 05 11:52 PM
How to programmatically determine which rows are outlined using Excel VBA code David Excel Programming 1 February 26th 05 02:29 AM
Excel VBA - How do I programmatically change source code in another file? Lisa Norgaard Excel Programming 1 July 14th 04 04:09 PM
programmatically open VBE and go to a specified procedure in target module? DataFreakFromUtah Excel Programming 4 July 12th 04 05:37 PM
Add reference programmatically before a module compiles Wexler Excel Programming 0 June 14th 04 05:18 PM


All times are GMT +1. The time now is 10:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"