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

问题描述

解锁受密码保护的 VBA 项目 中提供的代码适用于32 但不是 64 位.具体来说,

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

Ret = FindWindow(vbNullString, "VBAProject Password")

总是返回0.我试过了

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

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

没有任何作用.我还尝试了以下 API:setforegroundwindow、bringwindowtotop、setfocus、getactivewindow、getwindowthreadprocessid.仍然失败,所以我认为这不是焦点问题.也许是时候了?我尝试使用

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

但我是 API 的新手,所以不确定如何实现.

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

我通过个人.xlsb 中的 workbookopen 应用程序事件调用此方法 我试图在打开任何工作簿时填写密码.

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

在适用的情况下,我已将声明设置为 PtrSafe 和 LongPtr.

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

推荐答案

想通了.问题不是 64 位,而是时间/焦点问题.

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 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:26