使用 vba 和 windows api 通过 Excel 保存 hexeditor 文件
Posted
技术标签:
【中文标题】使用 vba 和 windows api 通过 Excel 保存 hexeditor 文件【英文标题】:Saving a hexeditor file through Excel using vba and windows api 【发布时间】:2014-12-05 13:33:58 【问题描述】:我需要这方面的帮助,我对 Windows API 了解不多,我正在尝试弄清楚如何保存文件并覆盖它(已经存在同名文件)。
我正在做的是:首先找到应用程序,发送一些像alt这样的键,一些向下键,回车。(文件->另存为->回车) 当我按下回车键时,会出现一个新窗口,询问有关保存文件的确认。 然后excel冻结,没有找到“确认另存为窗口”并按“是”。 它发生在命令中:
Call SendMessage(SaveButton, BM_CLICK, 0&, ByVal 0&)
谁能帮我解决这个问题。是因为它的焦点仍在另存为窗口之类的吗? 我应该使用其他方式来做我想做的事吗?
Public Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Public Declare PtrSafe Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare PtrSafe Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Public Declare PtrSafe Function SetActiveWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdSHow As Long) As Long
Public Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal lngHWnd As Long) As Long
Public Declare PtrSafe Function EnableWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal fEnable As Long) As Long
Public Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Public Declare PtrSafe Function GetFocus Lib "user32.dll" () As Long
Public Declare PtrSafe Function SendDlgItemMessage Lib "user32.dll" Alias "SendDlgItemMessageA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_CLOSE As Long = &H10
Public Const SW_SHOW As Integer = 5
Public Const WM_SETTEXT As Long = &HC
Public Const BM_CLICK As Long = &HF5&
Sub PulseAutomation()
CCPUlse = FindWindow(vbNullString, "HxD - [C:\Users\Matthew\Desktop\changelog.txt]")
view13844BringWindowToTop = BringWindowToTop(CCPUlse)
DoEvents
SendKeys "%", True
SendKeys "DOWN", True
SendKeys "DOWN", True
SendKeys "DOWN", True
SendKeys "DOWN", True
SendKeys "DOWN", True
SendKeys "~", True
Application.Wait (Now + #12:00:01 AM#)
SaveAsWindow = FindWindow(vbNullString, "Save As")
SaveButton = FindWindowEx(SaveAsWindow, 0&, "Button", "&Save")
Call EnableWindow(SaveButton, True)
Call SendMessage(SaveButton, BM_CLICK, 0&, ByVal 0&)
DoEvents
Application.Wait (Now + #12:00:01 AM#)
Dim hWndFind As Long
hWnd = FindWindow(vbNullString, "Confirm Save As")
'get the first child window with the class "Edit" (a textbox to VB)
hWndFind1 = FindWindowEx(hWnd, 0, "DirectUIHWND", vbNullString)
hWndFind2 = FindWindowEx(hWndFind1, 0, "CtrlNotifySink", vbNullString)
hWndFind3 = GetNextWindow(hWndFind2, GW_HWNDNEXT)
hWndFind4 = GetNextWindow(hWndFind3, GW_HWNDNEXT)
hWndFind5 = GetNextWindow(hWndFind4, GW_HWNDNEXT)
hWndFind6 = GetNextWindow(hWndFind5, GW_HWNDNEXT)
hWndFind7 = GetNextWindow(hWndFind6, GW_HWNDNEXT)
hWndFind8 = GetNextWindow(hWndFind7, GW_HWNDNEXT)
hWndFind9 = FindWindowEx(hWndFind8, 0, "Button", vbNullString)
Call SendMessage(hWndFind9, BM_CLICK, 0&, ByVal 0&)
End Sub
感谢您的建议和时间!
【问题讨论】:
【参考方案1】:为什么要这样保存文件而不是通过 VBA 方法:
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
沃特
【讨论】:
您好 wouter,谢谢您的回答。我不是要保存 excel 文件,而是 hexeditor 文件 这确实让事情变得更加困难。而且我的解决方案是没用的,希望我没有给你任何虚假的希望。您可以编辑您的帖子以更清楚地说明您想要实现的目标吗?通过 Excel 或其他程序保存 hexeditor 文件?【参考方案2】:解决方案:我使用 PostMessage 而不是 SendMessage,用于保存按钮控制。 还是谢谢!
【讨论】:
以上是关于使用 vba 和 windows api 通过 Excel 保存 hexeditor 文件的主要内容,如果未能解决你的问题,请参考以下文章
GetWindowsDirectory() API 返回错误 (vba\vb6)
HTML 图像未显示在通过 VBA 中的 Gmail API 发送的 MIME 消息中,使用 3D