Excel vba open file using default application in Windows 10 64 bit laptop, via ShellExcecute?

With excel 2016, I browse for my doc file (eg .docx, pdf, etc) or graphic file (eg .jpg, gif, pttx, etc) via Application.FileDialog(msoFileDialogFilePicker)
and wish to open it with my pc's default application.

This showed me a ShellExcecute API procedure

for this is have a reference for a 32-bit machine:

but my pc is 64-bit.

Please can you point me to the appropriate solution. (I am not 'agile' with API scripting, more 'cut and paste')
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try
#If Win64 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As LongLong, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongLong) As LongLong
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If

Open in new window

EDIT corrected code


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin4Author Commented:
Instant response - thanks.
      Always encouraged when I get your comments.
       I find I cant follow this up practically for 24 hours.
I give notice that I am actively engaged with your comment.
Kelvin4Author Commented:
Hi @Rgonzo
I dont understand the directions in:

'Step-by-Step Example

The following example demonstrates how to start an application or load a document into its associated application. The Windows API ShellExecute() function is different from the Visual Basic Shell() function in that you can pass the ShellExecute() function the name of a document and it will launch the associated application, and then pass the filename to the application.

Start a New project in Visual Basic. Form1 is created by default.
Add the following code to the General Declarations section of Form1:  '

I continue:
the procedure starts from    Private Sub Form_Click()   which looks like a click event for a userForm.
--- does that mean that 'Form1' at the end of step-by-step is in fact UserForm1?

Thinking 'YES' I created UserForm1 and put the API code in the UserForm code sheet.

I ran Userform1 from a standard code module: UserForm1.Show   ... which it did, but nothing else happened when I clicked the UserForm.

Can you comment further?
I attach my current file

Thanks! Kelvin
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

NorieAnalyst Assistant Commented:

Those instructions are for VB not VBA.

If you do want to use those instructions try using UserForm_Click instead of Form_Click.
Dim r As Long, msg As String

    r = StartDoc("C:\WINDOWS\ARCADE.BMP")
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                msg = "File not found"
            Case SE_ERR_PNF
                msg = "Path not found"
            Case SE_ERR_ACCESSDENIED
                msg = "Access denied"
            Case SE_ERR_OOM
                msg = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                msg = "DLL not found"
            Case SE_ERR_SHARE
                msg = "A sharing violation occurred"
                msg = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                msg = "DDE Time out"
            Case SE_ERR_DDEFAIL
                msg = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                msg = "DDE busy"
            Case SE_ERR_NOASSOC
                msg = "No association for file extension"
            Case ERROR_BAD_FORMAT
                msg = "Invalid EXE file or error in EXE image"
            Case Else
                msg = "Unknown error"
        End Select
        MsgBox msg
    End If
End Sub

Open in new window

PS How do you actually want to run the code? Pretty sure it's not by clicking a userform.:)

You mention browsing for files, do you have code for that already?
Kelvin4Author Commented:
Thank you, @Norie

I'm using Application.FileDialog(msoFileDialogFilePicker) to select one file at a time with
Debug.print "File path =  "; fDialog.SelectedItems(1)

So I would adapt your code above so that:
 r = StartDoc(fDialog.SelectedItems(1))

I'll test it out asap,

Kelvin4Author Commented:
Problem solved; files opening from file path alone.
Many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.