I am doing some scraping using excel vba and at some point I need to save the webpage of a specific content.
I am trying to simulate the Save As
window using Windows API in excel VBA and I could change the file name field so as to have the desired path
Private Declare PtrSafe Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Dim strBuff As String, butCap As String
Public Const WM_SETTEXT = &HC
Public Const BM_CLICK = &HF5
And this is the code I am using till now
Sub Sample(sPic As String)
'Dim hw As Long, hw2 As Long, hw3 As Long, saveRet As Long
Dim hw As Long, hw2 As Long, hw3 As Long, hw4 As Long, hw5 As Long, hw6 As Long, sv As Long, saveRet As Long
hw = FindWindow(vbNullString, "Save As")
hw2 = FindWindowEx(hw, 0&, "DUIViewWndClassName", vbNullString)
hw3 = FindWindowEx(hw2, 0&, "DirectUIHWND", vbNullString)
hw4 = FindWindowEx(hw3, 0&, "FloatNotifySink", vbNullString)
hw5 = FindWindowEx(hw4, 0&, "ComboBox", vbNullString)
hw6 = FindWindowEx(hw5, 0&, "Edit", vbNullString)
' Set the file name
Call SendMessageByString(hw6, &HC, 0, ThisWorkbook.Path & "\" & sPic & ".html")
' Click the Save button
saveRet = FindWindowEx(hw, 0, "Button", "&Save")
Call SendMessage(saveRet, BM_CLICK, 0, 0)
End Sub
This line works well Call SendMessageByString(hw6, &HC, 0, ThisWorkbook.Path & "\" & sPic & ".html")
and I can get the whole path in the file name field and also the save button at this line Call SendMessage(saveRet, BM_CLICK, 0, 0)
works well but when saving I didn't get my desired path but the default path.