gpt4 book ai didi

excel - Findwindow 在 64 位 VBA7 中不起作用

转载 作者:行者123 更新时间:2023-12-02 19:46:49 25 4
gpt4 key购买 nike

代码出现在 Unlocking Password Protected VBA project可以在 32 位下运行,但不能在 64 位下运行。具体来说,

Ret = FindWindow(vbNullString, "VBAProject Password")

总是返回0。我已经尝试过了

Ret = FindWindow("#32770", "VBAProject Password")

Ret = FindWindow("XLMAIN", "VBAProject Password")

没有一个有效。我还尝试了以下 API:setforegroundwindow、bringwindowtotop、setfocus、getactivewindow、getwindowthreadprocessid。仍然失败,所以我认为这不是一个焦点问题。也许是时机?我尝试使用

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(如果适用)。

最佳答案

想通了。问题不在于 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 If

关于excel - Findwindow 在 64 位 VBA7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com