繁体   English   中英

从文件夹中的所有工作簿中删除VBA代码

[英]Delete VBA code from all workbooks in a folder

我试图构建代码以远程循环文件夹与.xls文件并删除其中包含的宏。 到目前为止,我有各自的组件工作,但是在激活各种工作簿时遇到困难,然后以编程方式确保在每个文件中引用“Microsoft Visual Basic for Application Extensibility 5.3”。

谢谢!

Sub LoopFiles()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

strPath = ' enter path here

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strPath)

For Each objfile In objFolder.Files


If objFso.GetExtensionName(objfile.Path) = "xls" Then
   Set Objworkbook = objExcel.Workbooks.Open(objfile.Path)
    ' Include your code to work with the Excel object here
    Objworkbook.Activate

    AddReference (objfile)

   Objworkbook.Close True 'Save changes
End If

Next

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub




Sub AddReference(FileRequired)

     FileRequired.Activate
     'MsgBox "Sheet: " & ActiveWorkbook.Name

     ActiveWorkbook.VBProject.References.AddFromGuid _
     GUID:="{0002E157-0000-0000-C000-000000000046}", _
     Major:=5, Minor:=3
End Sub




Sub DeleteAllVBACode()

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Set VBProj = ActiveWorkbook.VBProject


        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                Set CodeMod = VBComp.CodeModule
                With CodeMod
                    .DeleteLines 1, .CountOfLines
                End With
            Else
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
    End Sub

就像我在评论中提到的那样,您不需要添加对Microsoft Visual Basic for Application Extensibility 5.3的引用来从文件中删除代码。 考虑这个小练习。

1 )。 创建一个Excel文件

2 )。 将此代码粘贴到模块中

Sub Sample1()
    MsgBox "A"
End Sub

3 )。 将上述文件另存为C:\\Sample.xls

4 )。 关闭文件

5 )。 打开一个新的Excel文件并将此代码粘贴到模块中

Option Explicit

'~~> Trust Access To Visual Basics Project must be enabled.
Sub Sample2()
    Dim wb As Workbook
    Dim i As Long

    '~~> Replace this with the relevant file
    '~~> We can open the files in a loop as well
    Set wb = Workbooks.Open("C:\Sample.xls")

    On Error Resume Next
    With wb.VBProject
        '~~> Remove the components
        For i = .VBComponents.Count To 1 Step -1
            .VBComponents.Remove .VBComponents(i)
        Next i
        '~~> Remove the code lines
        For i = .VBComponents.Count To 1 Step -1
            .VBComponents(i).CodeModule.DeleteLines _
            1, .VBComponents(i).CodeModule.CountOfLines
        Next i
    End With
    On Error GoTo 0
End Sub

6 )确保启用“信任访问Visual Basics项目”

7 )运行Sample2()

您将看到Sample.xls中的代码被删除,我们甚至没有设置对Microsoft Visual Basic for Application Extensibility 5.3的引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM