I am trying to save a PDF.
With the code below, I am to change the file Name but then after the SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
command the dialog box looses the focus and then I am unable to save the file.
From the web. I tried downloading directly from the URL but it does not work.
The command :
timeout = Now + TimeValue("00:00:20")
Do
hWnd = FindWindow(vbNullString, "Save As") 'Finding the save as window
DoEvents
Sleep 200
Loop Until hWnd Or Now > timeout
am using to get the handle for dialog box. If I write the same command after the
SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
I am able to get the focus of the dialog box but the command does not save the file with the changed file name. It saves it as the original name of OnlineStmtResultsPremDis.Do
Which looks like
How do I change the file name and save the pdf.
My Code: ****Navigating thru the webpage**** then
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Application.Wait (Now + TimeValue("00:0:03"))
IE.Navigate "https://www..com/CWRWeb/OnlineStmtResultsPremDis.do" 'Final PDF
Application.Wait (Now + TimeValue("00:0:18"))
Set HTMLDoc5 = IE.document
Application.SendKeys "+^{S}" 'Save Key ShortCut
Application.Wait (Now + TimeValue("00:0:03"))
'Finding the Save As Dialog Box
timeout = Now + TimeValue("00:00:20")
Do
hWnd = FindWindow(vbNullString, "Save As") 'Finding the save as window
DoEvents
Sleep 200
Loop Until hWnd Or Now > timeout
If hWnd Then
SetForegroundWindow hWnd
'Find the child DUIViewWndClassName window
hWnd = FindWindowEx(hWnd, 0, "DUIViewWndClassName", vbNullString)
End If
If hWnd Then
'Find the child DirectUIHWND window
hWnd = FindWindowEx(hWnd, 0, "DirectUIHWND", "")
End If
If hWnd Then
'Find the child FloatNotifySink window
hWnd = FindWindowEx(hWnd, 0, "FloatNotifySink", "")
End If
'If hWnd Then
'hWnd = FindWindowEx(hWnd, 0, "ComboBox", vbNullString) 'Child Combo Box
'End If
If hWnd Then
hWnd = FindWindowEx(hWnd, 0, "ComboBox", "") 'Child Combo Box
End If
If hWnd Then
SetForegroundWindow (hWnd) 'Chilf Edit Window
Sleep 600
hWnd = FindWindowEx(hWnd, 0, "Edit", "") 'Child Combo Box
End If
Sample = "80287.pdf" 'Misc. need to delete
If hWnd Then
SetForegroundWindow (hWnd) 'changing the folder name
Sleep 600
SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
End If
'Do
'hWnd = FindWindow(vbNullString, "Save As") 'Finding the save as window
'DoEvents
'Sleep 200
'Loop Until hWnd Or Now > timeout
If hWnd Then
SetForegroundWindow (hWnd)
hWnd = FindWindowEx(hWnd, 0, "Button", "&Save") 'Finding the Save button on the window
End If
If hWnd Then
SetForegroundWindow (hWnd) 'Click on the Save As window
Sleep 600
SendMessage hWnd, BM_CLICK, 0, 0
End If
End Sub
Declaration:
Option Explicit
Public Declare PtrSafe Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare PtrSafe Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, _
ByVal wParam As LongPtr, lParam As Any) As LongPtr
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As LongPtr
Public 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 LongPtr
Public Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPtr
Public Declare PtrSafe Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const BM_CLICK = &HF5
Public Const WM_SETTEXT = &HC
Public Const WM_GETTEXT = &HD
Public Const WM_GETTEXTLENGTH = &HE
Public Const VK_KEYDOWN = &H0
Public Const VK_KEYUP = &H2
Public Const VK_CONTROL = &H11