简体   繁体   English

打印已打开 Internet Explorer

[英]Print already opened Internet explorer

I am trying to print an Internet Explorer page that is already open but it does not work.我正在尝试打印已打开但无法正常工作的 Internet Explorer 页面。 Code below:下面的代码:

 Sub Already_Opened()

    On Error Resume Next
    Set objShell = CreateObject("Shell.application")
    intWinCnt = objShell.Windows.Count

    For intWinNo = 0 To (intWinCnt - 1)
      StrWinTitle = objShell.Windows(intWinNo).Document.Title

      If StrWinTitle = "Google" Then
        Set IE = objShell.Windows(intWinNo).Document
        Exit For

      End If

      Next
        Const OLECMDID_PRINT = 6
        Const OLECMDEXECOPT_DONTPROMPTUSER = 2

        'Search box
        IE.getElementsByClassName("gLFyf gsfi")(0).Value = "Earth"

        'Search result Button
        IE.getElementsByClassName("gNO89b")(0).Click

        'Wait 5 Sec
        Application.Wait Now + TimeValue("00:00:05")

        'Print
        IE.execWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

When I use Excel VBA to open and navigate to Internet explorer and then print it works.当我使用 Excel VBA 打开并导航到 Internet Explorer 然后打印它时,它可以工作。 Below code works:下面的代码有效:

Sub New_IE()

    Const OLECMDID_PRINT = 6
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2

    Dim IE As Object
    Dim Doc As HTMLDocument

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "https://www.google.co.uk/"

    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    Set Doc = IE.Document

    'Search box
    Doc.getElementsByClassName("gLFyf gsfi")(0).Value = "Earth"

    'Search result Button
    Doc.getElementsByClassName("gNO89b")(0).Click

    'Print
    Application.Wait Now + TimeValue("00:00:05")
    IE.execWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

End Sub

I try to check both sample codes and I found the cause for the issue.我尝试检查两个示例代码,并找到了问题的原因。

You said that the print code is working for the 2nd code example but not for the 1st code example.您说打印代码适用于第二个代码示例,但不适用于第一个代码示例。

If you check the line of code that prints the page then you can notice that you are using the IE browser object .如果您检查打印页面的代码行,您会注意到您使用的是IE 浏览器 object (in the 2nd example) (在第二个例子中)

If you see the 1st code sample then you can notice that you are assigning the IE document to the IE object .如果您看到第一个代码示例,那么您会注意到您正在将IE 文档分配给IE object

Set IE = objShell.Windows(intWinNo).Document

You need to assign the IE browser object instead of document object to fix the issue.您需要分配IE 浏览器 object而不是文档 object来解决此问题。

Below is the modified code that is working fine.下面是修改后的代码,它工作正常。

 Sub Already_Opened()
Dim IE As Object
    On Error Resume Next
    Set objShell = CreateObject("Shell.application")
    intWinCnt = objShell.Windows.Count

    For intWinNo = 0 To (intWinCnt - 1)
      StrWinTitle = objShell.Windows(intWinNo).document.Title

      If StrWinTitle = "Google" Then
        Set IE = objShell.Windows(intWinNo)
        Exit For

      End If

      Next
        Const OLECMDID_PRINT = 6
        Const OLECMDEXECOPT_DONTPROMPTUSER = 2

        'Search box
        IE.document.getElementsByClassName("gLFyf gsfi")(0).Value = "Earth"

        'Search result Button
        IE.document.getElementsByClassName("gNO89b")(0).Click

        'Wait 5 Sec
        Application.Wait Now + TimeValue("00:00:05")

        'Print
        IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

You can try to run this sample on your side and let us know whether it works for you or not.您可以尝试在您身边运行此示例,并让我们知道它是否适合您。 Let us know if anything is unclear.让我们知道是否有任何不清楚的地方。 I will try to provide suggestions for it.我会尽力为它提供建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM