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
https://stackoverflow.com/questions/18921168/how-can-excel-vba-open-file-using-default-application

for this is have a reference for a 32-bit machine:
https://support.microsoft.com/en-us/help/170918/how-to-use-shellexecute-to-launch-associated-file-32-bit

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')
Thanks!
Kelvin
Kelvin4Asked:
Who is Participating?
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.

Rgonzo1971Commented:
Hi,

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
#Else
    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

Regards
0

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:
@Rgonzo1971
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.
Kelvin
0
Kelvin4Author Commented:
Hi @Rgonzo
I dont understand the directions in:
https://support.microsoft.com/en-us/help/170918/how-to-use-shellexecute-to-launch-associated-file-32-bit

'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
1.-Shell-API-open-file-with-default.xlsm
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

NorieVBA ExpertCommented:
Kelvin

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"
            Case SE_ERR_ASSOCINCOMPLETE
                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?
0
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,
Thanks

Kelvin
0
Kelvin4Author Commented:
Problem solved; files opening from file path alone.
Many thanks
Kelvin
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.