VBA PDF TO EXCEL "Can't find project or Library"



我不知道该把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

在C:\Temp 目录中保存一个名为abc.PDF的PDF文件

向下滚动到代码底部,您会发现一个名为"测试"的子代码

确保代码看起来像这个

Sub TestPDF()
    OpenPDF "C:Temp" & "" & "abc.pdf", 1, 100
End Sub

最新更新