2016-07-19 5 views
0

Я не знаю, где разместить PDF на моем компьютере, мне нужно указать, что он находится по определенному пути, но я не уверен, как это сделать или изменить что в этом коде я нашел онлайн.VBA PDF TO EXCEL «Не удается найти проект или библиотеку»

Это дает мне ошибку в мин точке, но я не уверен, где в коде вы можете изменить путь или я не знаю, где путь даже поместить файл в формате 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 
+2

* ошибка в мин. Точке * - что такое * мин. Точка *? Также вам нужно передать 'FileName' в путь ...' OpenPDF («C: \ MyPDF.pdf», «2», «100») ' –

+3

, где вы объявили API? – cyboashu

+0

Я не включил их, но они находятся прямо над ними: –

ответ

0

Сохраните файл PDF под названием abc.pdf в директории C: \ Temp

прокрутки вниз к нижней части коды вы найдете саб под названием `Test»

убедитесь, что код выглядит следующим образом

Sub TestPDF() 

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

End Sub 
+0

Я сделал именно это, и он говорит, что ошибка компиляции: не удается найти проект или библиотеку –

+1

добавить VBA перед MID, 'VBA.Mid (strPDFPath, InStrRev (strPDFPath," \ ") + 1, Len (strPDFPath)) 'и проверьте, нет ли у вас ссылки unet Tools -> References. – cyboashu

+0

теперь он говорит, что путь PDF является инкорпорированным –

Смежные вопросы