在我的 Access 資料庫中,我在表單上有一個用于打開外部檔案的按鈕。這是我為此使用的代碼。
Private Sub btn_OpenFile_Click()
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
strPath = Chr(34) & strPath & Chr(34)
Call a.ShellExecute(Me.Attachment)
'Call CreateObject("Shell.Application").ShellExecute(strPath)
'MsgBox strPath
End Sub
我遇到的問題是,如果我實際輸入變數(Me.Attachment)的值,它可以正常作業并打開程式和檔案。
例如,如果我輸入 Call a.ShellExecute("C:\Docs\Some File.pdf") 它將打開。但是如果我在它的位置使用變數,它就不會打開并告訴我它找不到檔案。我已經用 msgbox 驗證它正在接收正確的資訊。我試圖用引號括起來并使用了 Chr(34),如上所示,但沒有任何效果。
如何讓該變數在 ShellExcute 命令中作業?
我瀏覽了所有論壇,似乎每個人都在使用字串而不是變數。我不想只使用 shell 命令,因為我不想追蹤人們用來打開不同型別檔案的所有不同應用程式。將需要打開不同的檔案型別,我認為這會比實際更容易。
感謝您的幫助。
uj5u.com熱心網友回復:
我很確定它ShellExecute需要一個 Variant 引數,而不是一個字串。
所以試試這個:
Call a.ShellExecute(CVar(strPath))
或從一開始就使用 Variant 變數。
我在這里遇到了同樣的問題。
uj5u.com熱心網友回復:
以下兩項都對我有用:
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
Call a.ShellExecute(strPath)
Dim a As Shell
Dim strPath As String
strPath = Me.Attachment
Set a = CreateObject("Shell.Application")
a.ShellExecute strPath
甚至直接參考附件。
a.ShellExecute(Me.Attachment)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/423088.html
標籤:
