Question : Code for Programmatically Removing VBA from Copied Workbook Not Working Properly

I used the copy command to copy a sheet from Workbook1 to Workbook2.  I am using the attached code to remove the VBA in the object (worksheet) module of the copied sheet.  Can anyone help me understand why the code does not recognize the VBComponents (the counter remains at zero); although, there are 13 procedures in the object module.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
Private Sub CommandButton5_Click()
'Run macro to trigger extract and save Recommendation sheet as separate Excel file
'-----------------------------------------------------
'Declare variables
'-----------------------------------------------------
    Dim wbNewBook As Workbook
    Dim fName As Variant
    Dim sourceWB As String
    Dim DGSSPkgNo As String
    Dim Alert As String
    Const pw As String = "delpdgsskey"
    
'---------------------------------------------------------
'Initialize variables for storing Buyer Input information
'---------------------------------------------------------
    DGSSPkgNo = Sheets("Project Header Sheet - PC").Cells(7, 3).Value
    fName = "C:\LeanDGSS\" & DGSSPkgNo & "_LeanWksht_SN"
    
    Application.EnableEvents = False
    On Error GoTo Terminate
'*************************************************************
'Prompt the user for a file name, and then saves the workbook
'*************************************************************
    If DGSSPkgNo = "" Then
        ' Define message
        Alert = MsgBox("The DGSS Project # must be entered" & _
        vbCrLf & "into the Project Header Sheet prior to Export", vbOKOnly, "Warning!")
        
        Application.EnableEvents = True
        Exit Sub
    Else
        ThisWorkbook.Worksheets("Recommended Supplier Data_SN").Copy
        ActiveWorkbook.SaveAs Filename:=fName
    End If
    Application.EnableEvents = True
 
'Call Module2 Procedure to remove VBA Components
    ActiveWorkbook.Sheets("Recommended Supplier Data_SN").Unprotect pw
    
    Dim objDocument As Workbook
    Set objDocument = ActiveWorkbook
    Call RemoveAllMacros(objDocument)
    
Exit Sub
Terminate:
    MsgBox "Error occurred: Verify file does not already exist in file path"
End Sub
'-----------------------------------------------------
Public Sub RemoveAllMacros(objDocument As Object)

    Dim i As Long, l As Long
    
    If objDocument Is Nothing Then Exit Sub
    
    i = 0
    On Error Resume Next
    
    i = objDocument.VBProject.VBComponents.Count
    On Error GoTo 0
    
    If i < 1 Then ' no VBComponents or protected VBProject
        MsgBox "The VBProject in " & objDocument.Name & _
        " is protected or has no components!", _
        vbInformation, "Remove All Macros"
        Exit Sub
    End If
    
    With objDocument.VBProject
    For i = .VBComponents.Count To 1 Step -1
        On Error Resume Next
        .VBComponents.Remove .VBComponents(i)
        ' delete the component
        On Error GoTo 0
    Next i
    End With
    
    With objDocument.VBProject
        For i = .VBComponents.Count To 1 Step -1
            l = 1
            On Error Resume Next
            l = .VBComponents(i).CodeModule.CountOfLines
            .VBComponents(i).CodeModule.DeleteLines 1, l
            ' clear lines
            On Error GoTo 0
        Next i
    End With
End Sub

Answer : Code for Programmatically Removing VBA from Copied Workbook Not Working Properly

Programmatic access, in outlook:

2003:
Application | tools | macros | Macro Security | trusted sources | Trust Access to the VBA Object Model ... Ensure ticked
2007:
Application | Office Button | App Options | Trust Centre | Trust Centre Settings | MAcro Settings | Trust Access to the VBA Object Model ... Ensure ticked
Random Solutions  
 
programming4us programming4us