我正在尝试构建代码以远程循环通过具有 .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