VBA PDF EX EXEL“无法找到项目或图书馆”

时间:2016-07-19 16:11:04

标签: excel vba

我不确定将PDF放在我的计算机上的哪个位置,我需要提交文件以便处于某个路径,但我不知道该怎么做或在我在网上找到的代码中进行更改。

它在最小点给我一个错误,但我不确定代码在哪里可以实际改变路径,或者我不确定路径甚至放在pdf文件的哪里

Option Explicit

'Constants used in API functions.
Public Const WM_SETTEXT = &HC
Public Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100

Private Sub OpenPDF(strPDFPath As String, strPageNumber As String, strZoomValue As String)

    'Opens a PDF file to a specific page and with a specific zoom
    'using Adobe Reader Or Adobe Professional.
    'API functions are used to specify the necessary windows
    'and send the page and zoom info to the Adobe window.

    'By Christos Samaras
    'http://www.myengineeringworld.net

    Dim strPDFName                  As String
    Dim lParent                     As Long
    Dim lFirstChildWindow           As Long
    Dim lSecondChildFirstWindow     As Long
    Dim lSecondChildSecondWindow    As Long
    Dim dtStartTime               As Date

    'Check if the PDF path is correct.
    If FileExists(strPDFPath) = False Then
        MsgBox "The PDF path is incorect!", vbCritical, "Wrong path"
        Exit Sub
    End If

    'Get the PDF file name from the full path.
    On Error Resume Next
    strPDFName = Mid(strPDFPath, InStrRev(strPDFPath, "\") + 1, Len(strPDFPath))
    On Error GoTo 0

    'The following line depends on the apllication you are using.
    'For Word:
    'ThisDocument.FollowHyperlink strPDFPath, NewWindow:=True
    'For Power Point:
    'ActivePresentation.FollowHyperlink strPDFPath, NewWindow:=True
    'Note that both Word & Power Point pop up a security window asking
    'for access to the specified PDf file.
    'For Access:
    'Application.FollowHyperlink strPDFPath, NewWindow:=True
    'For Excel:
    ThisWorkbook.FollowHyperlink strPDFPath, NewWindow:=True
    'Find the handle of the main/parent window.
    dtStartTime = Now()
    Do Until Now() > dtStartTime + TimeValue("00:00:05")
        lParent = 0
        DoEvents
        'For Adobe Reader.
        lParent = FindWindow("AcrobatSDIWindow", strPDFName & " - Adobe Reader")
        'For Adobe Professional.
        'lParent = FindWindow("AcrobatSDIWindow", strPDFName & " - Adobe Acrobat Pro")
        If lParent <> 0 Then Exit Do
    Loop

    If lParent <> 0 Then

        'Bring parent window to the foreground (above other windows).
        SetForegroundWindow (lParent)

        'Find the handle of the first child window.
        dtStartTime = Now()
        Do Until Now() > dtStartTime + TimeValue("00:00:05")
            lFirstChildWindow = 0
            DoEvents
            lFirstChildWindow = FindWindowEx(lParent, ByVal 0&, vbNullString, "AVUICommandWidget")
            If lFirstChildWindow <> 0 Then Exit Do
        Loop

        'Find the handles of the two subsequent windows.
        If lFirstChildWindow <> 0 Then
            dtStartTime = Now()
            Do Until Now() > dtStartTime + TimeValue("00:00:05")
                lSecondChildFirstWindow = 0
                DoEvents
                lSecondChildFirstWindow = FindWindowEx(lFirstChildWindow, ByVal 0&, "Edit", vbNullString)
                If lSecondChildFirstWindow <> 0 Then Exit Do
            Loop

            If lSecondChildFirstWindow <> 0 Then

                'Send the zoom value to the corresponding window.
                SendMessage lSecondChildFirstWindow, WM_SETTEXT, 0&, ByVal strZoomValue
                PostMessage lSecondChildFirstWindow, WM_KEYDOWN, VK_RETURN, 0

                dtStartTime = Now()
                Do Until Now() > dtStartTime + TimeValue("00:00:05")
                    lSecondChildSecondWindow = 0
                    DoEvents
                    'Notice the difference in syntax between lSecondChildSecondWindow and lSecondChildFirstWindow.
                    'lSecondChildSecondWindow is the handle of the next child window after lSecondChildFirstWindow,
                    'while both windows have as parent window the lFirstChildWindow.
                    lSecondChildSecondWindow = FindWindowEx(lFirstChildWindow, lSecondChildFirstWindow, "Edit", vbNullString)
                    If lSecondChildSecondWindow <> 0 Then Exit Do
                Loop
                If lSecondChildSecondWindow <> 0 Then

                    'Send the page number to the corresponding window.
                    SendMessage lSecondChildSecondWindow, WM_SETTEXT, 0&, ByVal strPageNumber
                    PostMessage lSecondChildSecondWindow, WM_KEYDOWN, VK_RETURN, 0

                End If

            End If

        End If

    End If

End Sub

Function FileExists(strFilePath As String) As Boolean

    'Checks if a file exists.

    'By Christos Samaras
    'http://www.myengineeringworld.net

    On Error Resume Next
    If Not Dir(strFilePath, vbDirectory) = vbNullString Then FileExists = True
    On Error GoTo 0

End Function

Sub TestPDF()

    OpenPDF ThisWorkbook.Path & "\" & "Sample File.pdf", 6, 143

End Sub

1 个答案:

答案 0 :(得分:0)

在目录C:\ Temp

中保存名为abc.pdf的PDF文件

向下滚动到代码的底部,您会找到一个名为“Test”的子

确保代码看起来像这样

Sub TestPDF()

    OpenPDF "C:\Temp" & "\" & "abc.pdf", 1, 100

End Sub