无法写入文件名并同时单击“保存”按钮...VBA代码
Posted
技术标签:
【中文标题】无法写入文件名并同时单击“保存”按钮...VBA代码【英文标题】:Unable to write the filename and click on Save button simultaneously...VBA code 【发布时间】: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?喜欢这里:***.com/questions/1858195/… 【参考方案1】:所以我更改了代码以在焦点位于编辑框时添加 PostMessage:代码有效,但我不确定如何....
If hWnd Then
'SetForegroundWindow (hWnd) 'changing the folder name
'Sleep 200
SendKeys "BACKSPACE"
Sleep 200
For x = 1 To Len(Sample)
lngPM = PostMessage(hWnd, WM_SETTEXT, Asc(Mid(Sample, x, 1)), 1&)
Sleep 200
Next x
Sleep 200
SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
End If
Application.Wait (Now + TimeValue("00:0:03"))
我正在使用 32 位 Ms excel 2016 Ms Office...windows 7 64 位...完整代码:
Do
hWnd = FindWindow("#32770", "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, "ComboBoxEx32", 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
Filename1 = Cells(I, 2).Value
Filename2 = Cells(I, 3).Value
Sample = "D:\Test\" & Filename1 & "_" & Filename & "_" & Filename2 & ".pdf" 'CHANGE : FILE NAME
If hWnd Then
'SetForegroundWindow (hWnd) 'changing the folder name
'Sleep 200
SendKeys "BACKSPACE"
Sleep 200
For x = 1 To Len(Sample)
lngPM = PostMessage(hWnd, WM_SETTEXT, Asc(Mid(Sample, x, 1)), 1&)
Sleep 200
Next x
Sleep 200
SendMessageByString hWnd, WM_SETTEXT, Len(Sample), Sample
End If
Application.Wait (Now + TimeValue("00:0:03"))
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
Label1:
IE.Quit
Application.Wait (Now + TimeValue("00:0:10"))
I = I + 1
Loop
End Sub
【讨论】:
以上是关于无法写入文件名并同时单击“保存”按钮...VBA代码的主要内容,如果未能解决你的问题,请参考以下文章