【发布时间】:2016-06-24 22:17:47
【问题描述】:
我正在尝试保存 PDF。
使用下面的代码,我将更改文件名,但在 SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample 之后
命令对话框失去焦点,然后我无法保存文件。
来自网络。我尝试直接从 URL 下载,但它不起作用。
命令:
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
用于获取对话框的句柄。如果我在
之后写相同的命令SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
我能够获得对话框的焦点,但该命令不会使用更改的文件名保存文件。它将它保存为 OnlineStmtResultsPremDis.Do 的原始名称 看起来像
如何更改文件名并保存 pdf。
我的代码: ****然后浏览网页****
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
声明:
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
【问题讨论】:
-
我不明白您是如何进入“另存为”窗口的。您是手动完成还是在某处调用 SaveAs?
-
Application.SendKeys "+^{S}" .... 使用这个快捷键 ctrl+shift+s
-
也许你应该试试这个How do i download a file using VBA。
-
保存后可以更改文件名吗?简单的重命名/移动命令?
-
为什么不使用 SaveAs 功能而不是 SendKeys?喜欢这里:stackoverflow.com/questions/1858195/…
标签: vba excel savefiledialog