在Unlocking Password Protected VBA project上显示的代码工作在32位以下,而不是64位。具体来说,
Ret = FindWindow(vbNullString, "VBAProject Password")始终返回0。我试过了
Ret = FindWindow("#32770", "VBAProject Password")
Ret = FindWindow("XLMAIN", "VBAProject Password")都不管用。我还尝试了以下API:setfore背景窗口、带窗口窗口、setfocus、getactivewindow、getactivewindow线程following。还是失败了所以我不认为这是个焦点问题。也许是时机问题?我试着用
SetTimer但是我对API并不熟悉,所以我不知道如何实现它。
我是通过我的personal.xlsb内的workbookopen应用程序事件调用这个程序的,我试图在打开任何工作簿时输入密码。
Private Sub AppEvent_WorkbookOpen(ByVal wb As Excel.Workbook)
If wb.VBProject.Protection <> 1 Then
Exit Sub
End If
Call unlockvba(wb)
End Sub编辑:如果适用的话,我已经将声明设置为PtrSafe和LongPtr。
发布于 2015-06-09 16:26:03
弄明白了。问题不是64位,而是时间/焦点问题。
Sub unlockVBA(wb)
If wb.name = "PERSONAL.XLSB" Then Exit Sub
Application.VBE.MainWindow.Visible = True
TimerID = SetTimer(0&, 0&, 500&, AddressOf TimerProc)
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
End Sub
Public Sub TimerProc(ByVal hwnd As LongPtr, ByVal wMsg As LongPtr, ByVal idEvent As LongPtr, ByVal dwTime As LongPtr)
On Error Resume Next
KillTimer hwnd, TimerID
MyPassword = "password"
Ret = FindWindow(vbNullString, "VBAProject Password")
ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)
If ChildRet <> 0 Then
Call SendMessage(ChildRet, WM_SETTEXT, False, ByVal MyPassword)
DoEvents
End If
ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
If ChildRet <> 0 Then
'MsgBox "Button's Window Found"
'~~> Get the caption of the child window
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr(0))
GetWindowText ChildRet, strBuff, Len(strBuff)
ButCap = strBuff
'~~> Loop through all child windows
Do While ChildRet <> 0
'~~> Check if the caption has the word "OK"
If InStr(1, ButCap, "OK") Then
'~~> If this is the button we are looking for then exit
OpenRet = ChildRet
Exit Do
End If
'~~> Get the handle of the next child window
ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
'~~> Get the caption of the child window
strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr(0))
GetWindowText ChildRet, strBuff, Len(strBuff)
ButCap = strBuff
Loop
If OpenRet <> 0 Then
'~~> Click the OK Button
SendMessage ChildRet, BM_CLICK, 0, vbNullString
Else
MsgBox "The Handle of OK Button was not found"
End If
Else
MsgBox "Button's Window Not Found"
End Ifhttps://stackoverflow.com/questions/30628817
复制相似问题