简体   繁体   English

尝试使用 ShellExecute 从 Access 中打开带有按钮的文件

[英]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.感谢您的帮助。

I'm pretty sure that ShellExecute expects a Variant parameter, not a String.我很确定ShellExecute需要一个 Variant 参数,而不是一个字符串。

So try this:所以试试这个:

Call a.ShellExecute(CVar(strPath))

or use a Variant variable from the start.或从一开始就使用 Variant 变量。

I had the same problem here .我在这里遇到了同样的问题。

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.

相关问题 访问 - 添加按钮以打开外部文件 - Access - Add button to open external file 用户单击表单上的按钮时,从存储在访问数据库中的路径打开文件 - open file from path stored in access db when user clicks button on form 如何从 MS Access 打开 Excel 文件? - How to open an Excel file from MS Access? 访问:尝试打开一个表单并使用另一个表单中的值填充文本框 - Access: trying to open a form and populate a textbox with the value from another form 无法使用 DoCmd.TransferSpreadsheet 打开从 Access 2010 导出的 .xlsx 文件 - Unable to open .xlsx file exported from Access 2010 using DoCmd.TransferSpreadsheet 从Access打开Excel文件并将图片复制到Access窗体 - Open Excel file from Access and copy a picture to the Access form 尝试使用pywin32打开MSProject mpp文件时出现“项目无法打开文件” - 'Project cannot open the file' when trying to open MSProject mpp file by using pywin32 尝试使用Windows API从电子表格打开音频文件 - Trying to open audio files from spreadsheet using windows API 如何使用VB在Word中创建命令按钮以打开文件 - How to make a command button in word open a file using VB 使用批处理文件打开时运行 Access 宏 - Running Access Macro whilst open using Batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM