在excel中自动打印但没有任何React

9nvpjoqh  于 2023-02-25  发布在  React
关注(0)|答案(1)|浏览(116)

我正在尝试将此代码用于以下目的:
当我点击Excel中的链接时,它会自动打印,但什么也没发生。
我已经安装了mozilla,路径是一样的。这东西的原因可能在哪里?

Sub PrintWebPage()
    Dim ie As Object
    Dim url As String
    Dim cell As Range
    ' Check if a hyperlink is selected
    On Error Resume Next
    Set cell = ActiveCell.Hyperlinks(1).Range
    If cell Is Nothing Then
        MsgBox "Please select a hyperlink."
        Exit Sub
    End If
    On Error GoTo 0
    ' Get the URL from the hyperlink
    url = cell.Hyperlinks(1).Address
    ' Open the internet page in Google Chrome
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate url
    ' Wait for page to load
    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop
    ' Print the page
    ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    ' Close Google Chrome
    ie.Quit
    Set ie = Nothing
End Sub

代码我已经张贴以上.

7bsow1i6

7bsow1i61#

这是你的例程的一个重写,这次没有任何错误忽略混淆的东西。你也可以检查VB编辑器中的即时窗口调试任何问题。

Sub PrintWebPage_v2()

    'declarations
    Dim ie As Object
    Dim URL As String
    
    Debug.Print "Running routine."
    Debug.Print "Cell selected: " & ActiveCell.Address
    Debug.Print "Workbook: " & ActiveWorkbook.Name & " on sheet " & ActiveSheet.Name

    With ActiveCell
        
        'check hyperlink exists
        If .Hyperlinks.Count = 0 Then
            Debug.Print "No hyperlinks found. Quitting."
            MsgBox "Please select a hyperlink."
            Exit Sub
        End If
        
        'grab URL
        URL = .Hyperlinks(1).Address
        Debug.Print "Hyperlink found: " & URL
        
        'Open IE
        Set ie = CreateObject("InternetExplorer.Application")
        Debug.Print "IE opened."
        ie.Visible = True
        
        'Tell IE to open URL
        Debug.Print "Requesting URL"
        ie.Navigate URL
    
        'Allow IE to complete
        Do While ie.Busy Or ie.readyState <> 4
            DoEvents
        Loop
        Debug.Print "IE complete"
        
         ' Print the page
        ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
        Debug.Print "Print command sent."
        
        ' Quit IE
        ie.Quit
        Debug.Print "IE closed."
        
    End With

End Sub

上面的命令中我唯一不能测试的是ie.ExecWB OLECMDID_PRINT命令,其余的都能正常工作,比如打开PDF等。

相关问题