Hi Holger,
I have used this code which is similar to yours, however it runs perfect with file names when I do F8, but when I just give F5 and run the code, it just clicks on save automatically with the default file name.
Is there anything wrong in the code.
Public Sub pdfsave()
On Error GoTo Whoa
Dim timeout As Date
SP = Worksheets("Input").Range("N1").Value
Ret = FindWindow(vbNullString, "Save PDF File As")
timeout = Now + TimeValue("00:00:50")
Do
Ret = FindWindow(vbNullString, "Save PDF File As")
' hwnd = FindWindow("#32770", "File Download")
DoEvents
Sleep 200
Loop Until Ret Or Now > timeout
SendKeys ("%{tab}")
If Ret <> 0 Then
'MsgBox "Main Window Found"
'~~> Get the handle of the Button's "Window"
ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
If ChildRet = 0 Then
MsgBox "Child Window Not Found"
Exit Sub
End If
'~~> 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 "Save"
If InStr(1, ButCap, "Save") 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
'~~> Check if we found it or not
If OpenRet = 0 Then
MsgBox "The Handle of Save Button was not found"
Exit Sub
End If
'~~> Retrieve the dimensions of the bounding rectangle of the
'~~> specified window. The dimensions are given in screen
'~~> coordinates that are relative to the upper-left corner of the screen.
GetWindowRect OpenRet, pos
'~~> Move the cursor to the specified screen coordinates.
SetCursorPos (pos.Left - 10), (pos.Top - 10)
'~~> Suspends the execution of the current thread for a specified interval.
'~~> This give ample amount time for the API to position the cursor
Sleep 200
SetCursorPos pos.Left, pos.Top
Sleep 200
SetCursorPos (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2
'~~> Set the size, position, and Z order of "File Download" Window
SetWindowPos Ret, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
Sleep 200
'~~> Simulate mouse motion and click the button
'~~> Simulate LEFT CLICK
mouse_event MOUSEEVENTF_LEFTDOWN, (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2, 0, 0
Sleep 700
'~~> Simulate Release of LEFT CLICK
mouse_event MOUSEEVENTF_LEFTUP, (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2, 0, 0
Wait 5
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' START OF SAVEAS ROUTINE '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Ret = FindWindow(vbNullString, "Save PDF File As")
' timeout = Now + TimeValue("00:00:50")
' Do
' Ret = FindWindow(vbNullString, "Save PDF File As")
'' hwnd = FindWindow("#32770", "File Download")
' DoEvents
' Sleep 200
' Loop Until Ret Or Now > timeout
'
If Ret = 0 Then
MsgBox "Save As Window Not Found"
Exit Sub
End If
'~~> UNCOMMENT this if using IE6 and COMMENT the code for "DUIViewWndClassName"
'~~> "DirectUIHWND" and "FloatNotifySink"
' '~~> Get the handle of the Main ComboBox
' ChildRet = FindWindowEx(Ret, ByVal 0&, "ComboBoxEx32", "")
'
' If ChildRet = 0 Then
' MsgBox "ComboBoxEx32 Window Not Found"
' Exit Sub
' End If
ChildRet = FindWindowEx(Ret, ByVal 0&, "DUIViewWndClassName", "")
If ChildRet = 0 Then
MsgBox "DUIViewWndClassName Not Found"
Exit Sub
End If
ChildRet = FindWindowEx(ChildRet, ByVal 0&, "DirectUIHWND", "")
If ChildRet = 0 Then
MsgBox "DirectUIHWND Not Found"
Exit Sub
End If
ChildRet = FindWindowEx(ChildRet, ByVal 0&, "FloatNotifySink", "")
If ChildRet = 0 Then
MsgBox "FloatNotifySink Not Found"
Exit Sub
End If
'~~> Get the handle of the Main ComboBox
ChildRet = FindWindowEx(ChildRet, ByVal 0&, "ComboBox", "")
If ChildRet = 0 Then
MsgBox "ComboBox Window Not Found"
Exit Sub
End If
'~~> Get the handle of the Edit
ChildRet = FindWindowEx(ChildRet, ByVal 0&, "Edit", "")
If ChildRet = 0 Then
MsgBox "Edit Window Not Found"
Exit Sub
End If
'~~> COMMENT the below 3 lines if you do not want to specify a filename
Wait 5
SendMess SP, ChildRet
Wait 5
'~~> Get the handle of the Save Button in the Save As Dialog Box
ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
'~~> Check if we found it or not
If ChildRet = 0 Then
MsgBox "Save Button in Save As Window Not Found"
Exit Sub
End If
'~~> 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 "Save"
If InStr(1, ButCap, "Save") 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
'~~> Check if we found it or not
If OpenRet = 0 Then
MsgBox "The Handle of Save Button in Save As Window was not found"
Exit Sub
End If
'~~> Save the file
SendMessage OpenRet, BM_CLICK, 0, ByVal 0&
Wait 5
Else
MsgBox "File Download Window Not found"
End If
Exit Sub
Whoa:
MsgBox Err.Description
End Sub