vba Windows api - Save as Freezes Excel

golemic

New Member
Joined
Mar 17, 2013
Messages
5
I need help on this, i don't know much about windows api, and i'm trying to figure out how i can save a file, and overwrrite it (already exists with the same name).


What i am doing is find the application first, send some keys like alt, some down keys, enter.(file->Save as->Enter)
When i press enter, it comes a new window asking confirmation about saving the file.
And then excel freezes, with out finding Confirm Save As window and pressing "Yes".
It happens in command:


Call SendMessage(SaveButton, BM_CLICK, 0&, ByVal 0&)


Can anyone help me solving this. Is it happening because it focus is still in save as window or something?
Should i use other way to do what i want?


Code:
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
Thanks for your advice and your time!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Thank you for your answer mr. Poulsom, and forgive me for my english.
What i'm trying to do, is through vba-excel save a HexEditor file, and overwrite it.
Is there a way to do it using OLE automation?
 
Upvote 0
Solution : I used PostMessage instead of SendMessage, for Save button control.
Thanks anyway!
 
Upvote 0

Forum statistics

Threads
1,216,725
Messages
6,132,340
Members
449,719
Latest member
excel4mac

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top