Something Similar From Excel VBA
I know this is about Access but what the heck? I did this to delete VBA modules from an Excel workbook a long time ago and it worked fine. Maybe it will point you in the general direction:
Function DeleteVBCode(wbDeliverable As Workbook) As Integer
'Deletes all VBA code from the workbook whose name is passed as a parameter.
'The following constants are as defined in Microsoft VBA help.
'Topic: Type Property (VBA Add-In Object Model).
'(Search on the word Type.)
'Use them to determine what type of VBA object a particular VBComponent is.
Const vbext_ct_StdModule As Long = 1 'Standard module
Const vbext_ct_ClassModule As Long = 2 'Class module
Const vbext_ct_MSForm As Long = 3 'Microsoft form
Const vbext_ct_ActiveXDesigner As Long = 11 'ActiveX Designer
Const vbext_ct_Document As Long = 100 'Document module
Dim n
Dim wb As Excel.Workbook
Dim objVbc As Object
Dim lnStLine As Long, lnDeclarationLines As Long
DeleteVBCode = Error 'Error condition
'Select workbook from which to delete VBA code.
For Each objVbc In wbDeliverable.VBProject.VBComponents
'Loop through the VBComponents and delete certain types.
'Debug.Print objVbc.Name, objVbc.Type
Select Case objVbc.Type
Case vbext_ct_StdModule, vbext_ct_ClassModule
'Delete the module.
wbDeliverable.VBProject.VBComponents.Remove objVbc
Case vbext_ct_Document
'This is code that goes with a worksheet or something similar.
'You can't remove this VBComponent, so just delete the individual
'lines of code.
lnDeclarationLines = objVbc.codemodule.CountOfDeclarationLines + 1
lnStLine = objVbc.codemodule.CountOfLines
If lnStLine < lnDeclarationLines Then
'The object contains no code. It's empty. Do nothing.
Else
'The object is not empty. Delete individual lines of code.
'You do it one line at a time.
'You use the Do Loop Until mode instead of a For Next loop
'because For Next won't work. The .DeleteLines doesn't always
'delete the entire line if the line is long and wraps to multiple
'screen line. Thus, if you have 100 lines of code and you
'remove 100 lines, you may actually have some lines left over,.
'fragments of the original 100.
Do
objVbc.codemodule.DeleteLines (1)
lnStLine = objVbc.codemodule.CountOfLines
Loop Until lnStLine = 0
End If
End Select
Next
Exit_DeleteVBCode:
DeleteVBCode = OK 'Results OK.
Set objVbc = Nothing
End Function
|