2017-12-20 6 views
0

웹 페이지를 탐색 한 후 PDF를 인쇄하려고합니다.IE.ExecWB 6,2는 인쇄 대화 상자를 표시합니다. 이 페이지를 인쇄하려면 어떻게합니까?

내가 인쇄 대화 상자에서 인쇄 버튼을 클릭 어떻게
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 

코드는 ableto 인쇄 대화 상자를 찾아 전경으로 설정됩니다. 그러나 인쇄 단추를 찾을 수 없습니다. FindWindowEx의 경우 hWnd를 0으로 반환합니다.

내 헤더 파일 :

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 

답변

1

this post에 따르면, 제 2 매개 변수 때문에, 인쇄 대화 상자의 동작을 제어합니다

IE.ExecWB 6, 1 

가 확인하지 않고 문서를 인쇄해야합니다.

+0

불행히도 인쇄 대화 상자 – Shank

0

이 예제에서는 ExecWB 명령을 호출 한 후 인쇄 단추를 클릭해야합니다. 당신은 대부분 거기있었습니다. API를 호출하는 방법과 반환되는 데이터에 문제가있는 것 같습니다 (예 : LongLongPtr). 플랫폼에 따라 정확한 API 호출을 지정하기위한 조건부 컴파일 조건을 추가했습니다. 나는 32 비트 Office를 실행하면서이 작업을 끝냈다.

Option Explicit 

Public Const BM_CLICK = &HF5 

#If VBA7 Then 
    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 LongPtr 
    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 
#Else 
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
    Public Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
    Public Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long 
    Public Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 
#End If 

Private Sub IEExample() 
    Const readyState_Complete As Byte = 4 
    Const printPage As Byte = 6 
    Const dontPrompt = 1 

    #If VBA7 Then 
     Dim windowHWND As LongPtr 
     Dim buttonHWND As LongPtr 
    #Else 
     Dim windowHWND As Long 
     Dim buttonHWND As Long 
    #End If 

    With CreateObject("InternetExplorer.Application") 
     .navigate "www.google.com" 
     While .busy Or .readystate <> readyState_Complete: Wend 
     .ExecWB printPage, dontPrompt 
    End With 

    windowHWND = getWindowPointer("Print", "#32770") 

    If windowHWND > 0 Then 
     'Add a small delay to allow the window to finish rendering if needed 
     Sleep 250 
     buttonHWND = FindWindowEx(windowHWND, 0, "Button", "&Print") 
     If buttonHWND > 0 Then 
      SendMessage buttonHWND, BM_CLICK, 0, 0 
     Else 
      Debug.Print "didn't find button!" 
     End If 
    End If 

End Sub 

Private Function getWindowPointer(WindowName As String, Optional ClassName As String = vbNullString) As Long 
    Dim i As Byte 

    #If VBA7 Then 
     Dim hwnd As LongPtr 
    #Else 
     Dim hwnd As Long 
    #End If 

    'Wait for the window to exist then return the pointer 
    For i = 1 To 100 
     hwnd = FindWindow(ClassName, WindowName) 
     If hwnd > 0 Then 
      getWindowPointer = hwnd 
      Exit For 
     End If 

     DoEvents 
     Sleep 200 
    Next 

End Function