有没有一种方法可以编写搜索,查找并在vba中打开?

时间:2019-06-05 12:53:33

标签: excel vba

我正在编写代码,但不知道语法是什么。我只希望我的代码搜索并找到pdf

Sub open1()

Dim pdfname As String
Const sPath = "S:\PROFILE ORDERS\"
Dim path1

pdfname = Application.InputBox("Enter the pdf you are looking for")
pdfname = pdfname & ".pdf"

path1 = Dir(sPath & pdfname)

path1.Open

End Sub

2 个答案:

答案 0 :(得分:1)

Sub OpenPdf()

    On Error GoTo OpenPdf_Error

    Dim pdfname As String
    Dim pdf
    Const sPath = "S:\RA QUOTES 2019"
    Dim FName As String
    Dim arNames() As String
    Dim myCount As Integer
    Dim i As Integer

    FName = Dir("S:\RA QUOTES 2019\*.pdf*")
    Do Until FName = ""
        myCount = myCount + 1
        ReDim Preserve arNames(1 To myCount)
        arNames(myCount) = FName
        FName = Dir
        Loop


    pdfname = Application.InputBox("Enter the pdf you are looking for")
    pdfname = "PLQ" & pdfname




For i = 1 To UBound(arNames)

If IsInArray(pdfname, arNames(i)) = True Then

    ThisWorkbook.FollowHyperlink sPath & arNames(i)

     End If

    Next i

    On Error GoTo 0
    Exit Sub

OpenPdf_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure OpenPdf"

End Sub

答案 1 :(得分:0)

就提供要在其中“搜索”的目录而言,这不是真正的搜索。几乎所有需要的内容都可能只在一行中:

ThisWorkbook.FollowHyperlink S:\PROFILE ORDERS\somePdf.pdf

其余的取决于您要如何处理。如果指定目录中没有这样的文件,则下面的代码将引发错误。

Sub OpenPdf()

    On Error GoTo OpenPdf_Error

    Dim pdfname As String
    Const sPath = "C:\Users\gropc\Desktop\"

    pdfname = Application.InputBox("Enter the pdf you are looking for")
    pdfname = pdfname & ".pdf"

    ThisWorkbook.FollowHyperlink sPath & pdfname

    On Error GoTo 0
    Exit Sub

OpenPdf_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure OpenPdf"

End Sub