Findwindow在64位VBA7中不起作用 [英] Findwindow does not work in 64bit VBA7

查看:370
本文介绍了Findwindow在64位VBA7中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解密密码保护的VBA项目中提供的代码在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应用程序调用这个我打算在任何工作簿上填写密码。

 私人子AppEvent_WorkbookOpen(ByVal wb As Excel.Workbook)

如果wb.VBProject.Protection<> 1然后
退出Sub
结束If

调用unlockvba(wb)

End Sub
pre>

编辑:我已经将声明设置为PtrSafe和LongPtr(如适用)。

解决方案

想出来这个问题不是64位,这是一个时间/焦点问题。

  Sub unlockVBA(wb)

如果wb.name =PERSONAL.XLSB,然后退出子

Application.VBE.MainWindow.Visible = True

TimerID = SetTimer(0& 0& 500& AddressOf TimerProc)

应用程序.VBE.CommandBars(1).FindControl(ID:= 2578,recursive:= True).Execute

End 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)
如果ChildRet& 0然后
调用SendMessage(ChildRet,WM_SETTEXT,False,ByVal MyPassword)
DoEvents
End If

ChildRet = FindWindowEx(Ret,ByVal 0&Button ,vbNullString)

如果ChildRet<> 0然后
'MsgBoxButton的窗口找到

'~~>获取子窗口的标题
strBuff = String(GetWindowTextLength(ChildRet)+ 1,Chr(0))
GetWindowText ChildRet,strBuff,Len(strBuff)
ButCap = strBuff

'~~>循环遍历所有子窗口
Do While ChildRet<> 0
'~~>检查标题是否有OK字样
如果InStr(1,ButCap,OK)然后
'~~>如果这是我们正在寻找的按钮,然后退出
OpenRet = ChildRet
退出Do
结束如果

'~~>获取下一个子窗口的句柄
ChildRet = FindWindowEx(Ret,ChildRet,Button,vbNullString)
'~~>获取子窗口的标题
strBuff = String(GetWindowTextLength(ChildRet)+ 1,Chr(0))
GetWindowText ChildRet,strBuff,Len(strBuff)
ButCap = strBuff
Loop

如果OpenRet<> 0然后
'~~>单击确定按钮
SendMessage ChildRet,BM_CLICK,0,vbNullString
Else
MsgBox没有找到OK按钮的句柄
如果
Else
MsgBoxButton not found
End If


The code presented at Unlocking Password Protected VBA project works under 32 but not 64bit. Specifically,

Ret = FindWindow(vbNullString, "VBAProject Password")

always return 0. I have tried

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

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

None works. I have also tried the following APIs: setforegroundwindow, bringwindowtotop, setfocus, getactivewindow, getwindowthreadprocessid. Still failed so I don't think it's a focus issue. Perhaps it's timing? I tried using

SetTimer

but I am new to APIs so not sure how to implement that.

I am calling this via the workbookopen app event inside my personal.xlsb I am trying to fill in password upon opening any workbook.

Private Sub AppEvent_WorkbookOpen(ByVal wb As Excel.Workbook)

If wb.VBProject.Protection <> 1 Then
Exit Sub
End If

Call unlockvba(wb)

End Sub

Edit: I have set my declarations to PtrSafe and LongPtr where applicable.

解决方案

Figured it out. The issue wasn't 64bit, it was a timing/focus issue.

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

这篇关于Findwindow在64位VBA7中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆