遍历文件夹以修改受保护的VBAProject中的代码

时间:2019-04-09 13:51:37

标签: excel vba password-protection excel-2016

我想遍历许多Excel宏文件并修改对代码的更改。所有这些文件/项目都受到已知密码的保护。我设法用密码打开了项目。挑战在于关闭“ VBAProject-项目属性”窗口,即,按“确定”按钮。我找到了Window处理程序,但是没有设法关闭窗口。实际上,它会在3-5秒后消失,但是延迟会在代码中带来后来的问题。 (宏挂起!)。它可以很好地处理几个文件,但是如果文件数超过五个,则宏会失败。这些文件位于Citrix.environment内部。

With objWordApp.VBE
    For Each VBProj In .VBProjects
    Debug.Print VBProj.FileName
        lCntr = lCntr + 1
        If VBProj.FileName = sPath Then
            Set .ActiveVBProject = .VBProjects(lCntr)
            objWordApp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
            strResult = GetHandleFromPartialCaption(lhWndP, "Password")
            ChildRet = FindWindowEx(lhWndP, ByVal 0&, "Edit", vbNullString)

            If ChildRet <> 0 Then
                SendMess MyPassword, ChildRet
                DoEvents
                hwnd = FindWindowEx(lhWndP, ByVal 0&, "BUTTON", "OK") 'vbNullString)
                If hwnd = 0 Then
                    Debug.Print "Couldn't find the button"
                End If
                Call SendMessageBynum(hwnd, BM_CLICK, 0, 0)

                strResult = GetHandleFromPartialCaption(lhWndP, "TemplateProject")
                DoEvents

                hwnd = FindWindowEx(lhWndP, ByVal 0&, "BUTTON", "OK") 'vbNullString)
                If hwnd = 0 Then
                    Debug.Print " TemplateProject Couldn't find the button " & sPath
                End If
                Call SendMessageBynum(hwnd, BM_CLICK, 0, 0)

            End If

            Set objProjectToBeedited = .ActiveVBProject

            Exit For
        End If
    Next
End With

0 个答案:

没有答案