【问题标题】:Open Active Link on a PDF Using VBA使用 VBA 打开 PDF 上的活动链接
【发布时间】:2020-06-21 16:16:05
【问题描述】:

我需要使用 VBA 打开位于特定网站上的 PDF,并使用位于 PDF 第一页上的链接之一导航到 PDF 中的特定页面。在 MS Access 的 VBA 中使用 FollowHyperlink 方法我可以打开 PDF。我在 FollowHyperlink 方法中输入了链接的名称作为子地址,但它仍然只将我带到打开页面。我有什么遗漏吗?

【问题讨论】:

  • 我们是否应该了解您需要访问 pdf 中的现有链接?您想使用标准 VBA 来实现吗?您的项目是否引用了Adobe Acrobat ... library
  • 那么,您想访问 pdf 文档的特定页面,还是真正点击其首页上的链接,指向另一个文档/URL?
  • 谢谢。 PDF 位于非我或我的客户维护的公共网站上。我无法改变它的结构。这是一个多页的pdf。 URL 转到第一页。第一页有 3 列,每个项目都有一个名称,后跟一个页码。名称和页码一起形成某种链接(链接或命名目的地 - 我不知道是哪个)。对于任何特定的搜索,我都不知道我需要的页码,但我知道页码之前的名称。我试过 Tim 和 June7 都没有成功。 FaneDuru - 按照第一页上的链接到同一 PDF 中的另一页

标签: vba ms-access pdf


【解决方案1】:

通过几个来源进行一些研究后,我发现我可以调用 Internet Explorer(IE 中存在 COM 功能,但不幸的是,从我能够找到的内容中,不在 Edge 中)并使用 SendKeys 打开所需的网页并导航到PDF 中包含所需信息的特定页面。

以下代码是适用于这种特殊情况的解决方案:

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal 
nCmdShow As Long) As Long

Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWNORMAL = 1
Const SW_HIDE = 0


Sub OpenPageToSelectedNameInfo()

    On Error GoTo Err_OPtSNI

    Dim strListLink As String
    Dim strSelectedName As String
    Dim objIE As New InternetExplorer

    strSelectedName = "Selected Name"
    strListLink = "https://www.publicsite.com/directory.pdf"

    With objIE
        .Navigate strListLink
        .Visible = True
        .Silent = False
    End With

    ShowWindow objIE.hwnd, SW_SHOWNORMAL

    WaitSeconds 4           '     Wait for PDF to load before summoning 
                            '     Find panel

    SendKeys "^f", True     '   Open Find panel on PDF
    SendKeys "^-", True     '   Reduce screen size one step
    SendKeys "^-", True     '   Reduce screen size one more step

    SendKeys strSelectedName, True

    SendKeys "{TAB}", True  '   Tab to Find panel settings
    SendKeys "w", True      '   Select Whole Words Only
    SendKeys "{TAB}", True  '   Tab out of settings

    WaitSeconds 2           '   Wait for whole words only to take affect

    SendKeys "^G", True     '   Find next entry - moves from directory page 
                            '   to page with data matching selected name


    Exit_OPtSNI:
        Exit Sub

    Err_OPtSNI:
        strErrMsg = "Error coming from Open Page to Selected Name Info. _
                    Err # " & Err.Number & " - " & Err.Description

        MsgBox strErrMsg
        Resume Exit_OPtSNI

End Sub

WaitSeconds 调用是基于 Sleep API 的单独过程,如下所示:

Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

Sub WaitSeconds(intSeconds As Integer)

    On Error GoTo Err_WS

    Dim strErrMsg As String
    Dim dteRelease As Date

    dteRelease = DateAdd("s", intSeconds, Now)

    Do
        Sleep 100
        DoEvents
    Loop Until Now >= dteRelease

    Exit_WS:
        Exit Sub

    Err_WS:
        strErrMsg = "Error coming from Wait Seconds. Err # " 
                   & Err.Number & " - " & Err.Description

        MsgBox strErrMsg
        Resume Exit_WS

End Sub

【讨论】:

    猜你喜欢
    • 2012-07-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多