简体   繁体   中英

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. 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.

For Example, If I put in Call a.ShellExecute("C:\Docs\Some File.pdf") it will open. 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. I have tried to wrap it in quotes and have used the Chr(34) as shown above but nothing works.

How can I get that variable to work in the ShellExcute command?

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. 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.

So try this:

Call a.ShellExecute(CVar(strPath))

or use a Variant variable from the start.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM