查找Excel宏的内容

时间:2015-11-12 15:45:20

标签: excel vba excel-vba powershell

我正在寻找一种浏览数百个Excel文件内容的方法,以查找并删除任何硬编码用户和密码的痕迹。

我希望在Powershell中使用一个函数,但到目前为止我还没有找到它。

1 个答案:

答案 0 :(得分:2)

您应该使用文件脚本对象see examples there

并结合引用VBIDE对象列出项目中的所有模块并且:

搜索模块中的文字

Sub SearchCodeModule()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim FindWhat As String
    Dim SL As Long ' start line
    Dim EL As Long ' end line
    Dim SC As Long ' start column
    Dim EC As Long ' end column
    Dim Found As Boolean

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents("Module1")
    Set CodeMod = VBComp.CodeModule

    FindWhat = "findthis"

    With CodeMod
        SL = 1
        EL = .CountOfLines
        SC = 1
        EC = 255
        Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
            EndLine:=EL, EndColumn:=EC, _
            wholeword:=True, MatchCase:=False, patternsearch:=False)
        Do Until Found = False
            Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC)
            EL = .CountOfLines
            SC = EC + 1
            EC = 255
            Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                EndLine:=EL, EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
        Loop
    End With
End Sub

来源:cpearson.com/excel/vbe.aspx