【发布时间】:2023-04-05 21:39:01
【问题描述】:
我在浏览网页后尝试打印 PDF。 下面是代码:
Sub login()
Dim IE As Object
Dim HTMLDoc As Object, HTMLDoc2 As Object
Dim objCollection As Object
Dim currentURL As String
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "url"
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Set HTMLDoc = IE.document
'Application.Wait (Now + TimeValue("00:0:10"))
With HTMLDoc
HTMLDoc.getElementById("userName").Value = "ABC" 'Entering credential
HTMLDoc.getElementById("password").Value = "xyz"
End With
Application.Wait (Now + TimeValue("00:0:3"))
Set objCollection = IE.document.getElementById("submitButton")
objCollection.Click
Do While IE.Busy Or IE.ReadyState <> 4: Loop ' opening the second webpage
Set HTMLDoc2 = IE.document
With HTMLDoc2
IE.Navigate "URL"
End With
Application.Wait (Now + TimeValue("00:0:10"))
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Set objCollection = IE.document.getElementById("menu_print")
objCollection.Click
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Application.Wait (Now + TimeValue("00:0:03"))
With CreateObject("Shell.Application").Windows
If .Count > 0 Then
' Get IE
Set IE = .Item(1) ' or .Item(.Count - 1) 'second tab
IE.Navigate "URL" 'third webpage
IE.ExecWB 6, 2
'Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End If
End With
End Sub
如何单击打印对话框上的打印按钮,或者如何在不单击或处理对话框的情况下在第三个网页上打印 PDF。是否有直接命令在网页上打印 PDF。
添加了查找打印对话框的 bleow 代码 代码:
timeout = Now + TimeValue("00:00:04")
Do
hWnd = FindWindow(vbNullString, "Print") '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)
hWnd = FindWindowEx(hWnd, 0, "Button", "&Print") 'Finding the Print button on the window
Sleep 600
SendMessage hWnd, BM_CLICK, 0, 0
End If
代码能够找到打印对话框并将其设置在前台。 但是,它找不到打印按钮。对于 FindwindowEx,它将 hWnd 返回为零。
我的头文件:
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 LongPtr
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
【问题讨论】: