[英]Trying to Open a File with a Button from Access using ShellExecute
In my Access database, I have a button on a form to open an external file.在我的 Access 数据库中,我在表单上有一个用于打开外部文件的按钮。 Here is the code that I am using for that.
这是我为此使用的代码。
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
The problem that I have is if I actually put in the value of the variable (Me.Attachment) it works fine and opens the program and the file.我遇到的问题是,如果我实际输入变量(Me.Attachment)的值,它可以正常工作并打开程序和文件。
For Example, If I put in Call a.ShellExecute("C:\Docs\Some File.pdf") it will open.例如,如果我输入 Call a.ShellExecute("C:\Docs\Some File.pdf") 它将打开。 But if I use the variable in it's place it won't open and tells me it cannot find the file.
但是如果我在它的位置使用变量,它就不会打开并告诉我它找不到文件。 I have verified with the msgbox that it is receiving the correct information.
我已经用 msgbox 验证它正在接收正确的信息。 I have tried to wrap it in quotes and have used the Chr(34) as shown above but nothing works.
我试图用引号括起来并使用了 Chr(34),如上所示,但没有任何效果。
How can I get that variable to work in the ShellExcute command?如何让该变量在 ShellExcute 命令中工作?
I have looked through all the forums and it seems like everyone is using a string but not a variable.我浏览了所有论坛,似乎每个人都在使用字符串而不是变量。 I don't want to use just the shell command as I don't want to track down all of the different apps people use to open different types of files.
我不想只使用 shell 命令,因为我不想追踪人们用来打开不同类型文件的所有不同应用程序。 There will be different file types that will need to be opened and I thought this would be easier than it actually is.
将需要打开不同的文件类型,我认为这会比实际更容易。
Thank you for the help.感谢您的帮助。
Both of the following work for me:以下两项都对我有用:
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)
Even referencing Attachment directly.甚至直接引用附件。
a.ShellExecute(Me.Attachment)
Either works with or without New
qualifier and with or without Shell32
prefix and with or without Call
.可以使用或不使用
New
限定符,使用或不Shell32
前缀,使用或不使用Call
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.