簡體   English   中英

如何通過excel宏打開帶有通配符選項的.PDF文件

[英]How to open a .PDF file with wild card option via excel macro

由於我對 excel 宏非常陌生,因此我正在嘗試開發一個能夠打開 PDF 文件的代碼。但是我的系統中有一些 PDF 文件是由另一個系統生成的,因此這些文件名每天都在變化,有些數字也包括在內。

例如,“處理報告 151120183569844”就像這樣。這些數字每天都在變化。我嘗試添加 WILDCARD 選項,但它不起作用。如何僅使用文件名的一部分打開此 PDF?

     Sub Open_PDF()
    Dim pdfPath As String
    pdfPath ="D:\Reports\Process Report*" & ".pdf" 'I used wildcard instead "Process Report 151120183569844"'
Call OpenAnyFile(pdfPath)
End Sub

Function openAnyFile(strPath As String)
Set objShell = CreateObject("Shell.Application")
objShell.Open(strPath)
End Function

正如在另一個答案中指出的那樣,帶有通配符的Dir函數應該可以解決問題。

這是使用原始openAnyFile函數的示例。

Sub Open_PDF()
    Dim filePath As String, fileName As String

    filePath = "D:\Reports\"
    fileName = Dir(filePath & "Process Report*.pdf")

    If fileName <> "" Then
        openAnyFile filePath & fileName
    End If
End Sub

Function openAnyFile(strPath As String)
    Dim objShell As Object
    Set objShell = CreateObject("Shell.Application")
    objShell.Open (strPath)
End Function

您不能使用通配符打開文件 - 這只是常識,如果有多個文件符合您的條件怎么辦 - 您希望編程打開哪個? 您必須指定確切的文件名才能打開它。

如果目標目錄中只有一個文件,則可以使用類似於以下代碼的內容來打開它,而不管其名稱是什么:

sFound = Dir(ActiveWorkbook.Path & "\Process Report*.xlsm") 

If sFound <> "" Then
    Workbooks.Open filename:= ActiveWorkbook.Path & "\" & sFound
End If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM