[英]Trying to Open a File with a Button from Access using ShellExecute
在我的 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 命令,因為我不想追蹤人們用來打開不同類型文件的所有不同應用程序。 將需要打開不同的文件類型,我認為這會比實際更容易。
感謝您的幫助。
我很確定ShellExecute
需要一個 Variant 參數,而不是一個字符串。
所以試試這個:
Call a.ShellExecute(CVar(strPath))
或從一開始就使用 Variant 變量。
我在這里遇到了同樣的問題。
以下兩項都對我有用:
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)
可以使用或不使用New
限定符,使用或不Shell32
前綴,使用或不使用Call
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.