3

.xlsファイルを含むフォルダーをリモートでループし、その中に含まれるマクロを削除するコードを作成しようとしています。これまでのところ、個々のコンポーネントは機能していますが、さまざまなブックをアクティブ化して、プログラムで「Microsoft Visual Basic forApplicationExtensibility5.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
4

1 に答える 1

4

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ます。

于 2013-02-08T01:39:58.180 に答える