首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找窗口在64位VBA7中不工作

查找窗口在64位VBA7中不工作
EN

Stack Overflow用户
提问于 2015-06-03 19:21:27
回答 1查看 3.5K关注 0票数 4

Unlocking Password Protected VBA project上显示的代码工作在32位以下,而不是64位。具体来说,

代码语言:javascript
复制
Ret = FindWindow(vbNullString, "VBAProject Password")

始终返回0。我试过了

代码语言:javascript
复制
Ret = FindWindow("#32770", "VBAProject Password")

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

都不管用。我还尝试了以下API:setfore背景窗口、带窗口窗口、setfocus、getactivewindow、getactivewindow线程following。还是失败了所以我不认为这是个焦点问题。也许是时机问题?我试着用

代码语言:javascript
复制
SetTimer

但是我对API并不熟悉,所以我不知道如何实现它。

我是通过我的personal.xlsb内的workbookopen应用程序事件调用这个程序的,我试图在打开任何工作簿时输入密码。

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-09 16:26:03

弄明白了。问题不是64位,而是时间/焦点问题。

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30628817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档