[英]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.