【问题标题】:Opening a list of pdf hyperlinks and closing them打开 pdf 超链接列表并关闭它们
【发布时间】:2020-07-01 14:38:58
【问题描述】:

我在 excel 中有一个 pdf 超链接列表,我正在尝试创建一个宏来查看超链接列表并打开它们以测试它们是否有效。该列表位于 sheet4.rowD 上。以下代码是我尝试过的,但一直抛出错误 400。

Sub Test_Template_Links()

With Sheet4
LastRow = Sheet4.Range("D999").End(xlUp).Row
For CustRow = 2 To 3 'LastRow

ThisWorkbook.FollowHyperlink Sheet4.Range("E" & CustRow).Value

Application.Wait 0.00002
Application.SendKeys "^(q)", True
Application.Wait 0.00001

Next CustRow

End With

End Sub

此外,如果有人知道在单元格不起作用时突出显示单元格并保持宏继续运行的方法,那将很棒,但现在没有必要。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这应该可以满足您的需求。您需要将 PDF 放在受信任的位置,否则每次尝试打开 PDF 时都会弹出一个窗口。除了受信任的位置之外,可能还有一种解决方法,但我不知道(并且通常不建议使用此类东西)。

    您还需要将Hwnd = FindWindow(vbNullString, filename & " - Foxit Reader") 更改为您正在使用的任何 PDF 阅读器,我有 Foxit,所以它现在就是这样。

    感谢 Siddharth Rout 提供closing of the PDF

    Option Explicit
    'Thanks to Siddharth Rout for this chunk
    
    #If VBA7 Then
        Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
        (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
        lParam As Any) As Long
        
        Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassname As String, ByVal lpWindowName As String) As Long
    #Else
        Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
        (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
        lParam As Any) As Long
        
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassname As String, ByVal lpWindowName As String) As Long
    #End If
    
    Private Const WM_CLOSE = &H10
    
    Sub t()
        Dim Tcell As Range
        Dim link As String
        Dim lastrow As Long
        Dim iter As Long
        With Sheet4
            lastrow = .Cells(.Rows.Count, 5).End(xlUp).Row
            For iter = 2 To lastrow
                Set Tcell = .Cells(iter, 5)
                On Error GoTo errhandler
                ThisWorkbook.FollowHyperlink Tcell.value
                On Error GoTo 0
                'Thanks to Siddharth Rout for this chunk
                Dim Hwnd As Long
                Dim filename As String
                filename = Split(Tcell.value, "\")(UBound(Split(Tcell.value, "\")))
                '~~> Find the window of the pdf file
                Hwnd = FindWindow(vbNullString, filename & " - Foxit Reader")
                '~~> Close the file
                PostMessage Hwnd, WM_CLOSE, 0, ByVal 0&
    continueiter:
            Next iter
        End With
        Exit Sub
    errhandler:
        Select Case Err.Number
            Case -2147221014
                Tcell.Interior.Color = vbRed
                GoTo continueiter
            Case Else
                MsgBox "Unhandled Error: " & Err.Number & chr(10) & Err.Description
        End Select
    End Sub
    

    【讨论】:

    • 当我尝试在 Sub t() 之前输入第一个块时,一切都是红色的,我不知道为什么
    • 你是复制粘贴还是打出来的?也许您在注释行中使用了双引号 (") 而不是单引号 (')?
    • 我尝试了复制/粘贴,但没有成功,所以我输入了它,但它仍然显示为红色错误。只有两条“Private Declare....”行它说“必须更新此项目中的代码才能在 64 位系统上使用。请查看并更新 Declare 语句,然后用 PtrSafe 属性标记它们。”跨度>
    • 好的,现在试试。
    • 现在我在link = Split(Tcell.Formula, Chr(34))(1)上得到一个超出范围的下标
    猜你喜欢
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2018-07-02
    • 2018-12-11
    • 2012-12-15
    • 1970-01-01
    • 2015-03-08
    • 2022-11-21
    相关资源
    最近更新 更多