Programmatically removing code from ThisWorkbook XL2007

P

PCLIVE

I'm still having some trouble with the code below. It previously worked in
XL2002 and earlier. However, there were some issues on some installations
of XL2003 and now consistant errors on XL2007.
The error occurs on line, "Set CodeMod =
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule"

If I attempt to step through the code, it just gives you an error stating,
"Run-time error '1004': Application-defined or object-defined error"

If I initiate this code from the button-click as intended, then I get the
"Run-time error '1004': Programmatic access to Visual Basic Project is
not trusted"

Is there a way to achieve what I want in XL2007?

Thanks,
Paul


Private Sub CommandButton1_Click()
Dim CodeMod As VBIDE.CodeModule
Dim StartLine As Long
Dim ProcLen As Long

Set CodeMod =
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
With CodeMod
StartLine = .ProcStartLine("Workbook_Open", vbext_pk_Proc)
ProcLen = .ProcCountLines("Workbook_Open", vbext_pk_Proc)
.DeleteLines StartLine, ProcLen
End With

End Sub

--
 
C

Chip Pearson

On the Office button, choose "Excel Options" then the "Trust Center"
page. There, click the "Trust Center Settings" button. Choose the
"Macro Setting" page and check the "Trust access to the VBA project
object model".

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
P

PCLIVE

Thanks Chip. That was it.



--

Chip Pearson said:
On the Office button, choose "Excel Options" then the "Trust Center"
page. There, click the "Trust Center Settings" button. Choose the
"Macro Setting" page and check the "Trust access to the VBA project
object model".

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top