-
May 12th, 2019, 08:56 PM
#1
Thread Starter
Fanatic Member
How to programmically open a file by an application
I need to programmically open a file by an application.
Let's say the file's path is stored in a variable named My_File_Path.
1. How can my program open a file by the application that is associated with it?
If the extension txt has been already associated with Notepad, and you double-click on a text file in Windows explorer, that file is opened in Notepad.
Now, if the logic of my program is such that it needs to open a file, how can I program it to open that file by the application that is associated with it?
2. Also, in Windows explorer, you can right-click on a file and choose "open with" and in that case, it provides you with a list of all the applications that can open that file.
How can I programmically do this one as well?
Thanks.
-
May 12th, 2019, 09:30 PM
#2
Re: How to programmically open a file by an application
The first, search for ShellExecute in these forms (an API call).
The second, you can do with Shell. Just specify the program and the data file in the shell string, possibly surrounding each with quotes. Also, you may need to specify the entire path to the program and the data file. ShellExecute will do this as well.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 13th, 2019, 01:29 AM
#3
Re: How to programmically open a file by an application
Shell32 has a perfectly good Automation interface:
Code:
Option Explicit
'ShowWindow() Commands:
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_NORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10
Private Const SW_FORCEMINIMIZE = 11
Private Const SW_MAX = 11
Private Sub Command1_Click()
CreateObject("Shell.Application").ShellExecute "sample.txt", , , "open", SW_SHOWNORMAL
End Sub
There are other ways to use the Shell32 object model, for example:
Code:
Const ssfDESKTOP = 0
With CreateObject("Shell.Application").NameSpace(ssfDESKTOP)
With .ParseName(CurDir$() & "\sample.txt") '<-- Full path of file.
.InvokeVerb "openas"
End With
End With
That one produces the "open with" dialog where the user can choose a tool from the existing list of them if any or else specify another tool.
I'm not aware of anything provided to let you retrieve the existing "open as" tool list though. You'd have to go registry spelunking for that as far as I know, and now you're into crazy territory anyway.
Both of the examples above open relative to the current directory, but in the second one we must provide the full path.
This stuff is covered in your October 2001 MSDN documentation CDs.
-
May 13th, 2019, 11:06 AM
#4
Fanatic Member
Re: How to programmically open a file by an application
dilettante,
I use this function:
Code:
ShellExecute Me.hWnd, vbNullString, App.Path & "\HelpFiles\Documentname.pdf", vbNullString, vbNullString, vbNormalFocus
Does it have restrictions that your CreateObject does not have?
PK
-
May 13th, 2019, 11:51 AM
#5
Re: How to programmically open a file by an application
The flat API call you appear to be using should give results identical to the Automation method call. The method call does not return a PID value, accepts Unicode String values, and doesn't have the overhead of doing several copy/ANSI-conversion operations.
When you use Declare with ByVal X As String each of your X arguments implies a Unicode-to-ANSI conversion before the actual call and then an ANSI-to-Unicode conversion after the call returns. The extra memory allocations, conversion operations, and deallocations don't amount to much, but why throw money away? It is even worse because when the call gets made the code in Shell32 converts the ANSI to Unicode to use it!
You can cut the overhead by using the W entrypoint instead of the A entrypoint and passing pointers. Or just use the Automation interface.
-
May 18th, 2019, 04:40 AM
#6
Thread Starter
Fanatic Member
Re: How to programmically open a file by an application
Originally Posted by dilettante
Shell32 has a perfectly good Automation interface:
Code:
Option Explicit
'ShowWindow() Commands:
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_NORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10
Private Const SW_FORCEMINIMIZE = 11
Private Const SW_MAX = 11
Private Sub Command1_Click()
CreateObject("Shell.Application").ShellExecute "sample.txt", , , "open", SW_SHOWNORMAL
End Sub
There are other ways to use the Shell32 object model, for example:
Code:
Const ssfDESKTOP = 0
With CreateObject("Shell.Application").NameSpace(ssfDESKTOP)
With .ParseName(CurDir$() & "\sample.txt") '<-- Full path of file.
.InvokeVerb "openas"
End With
End With
That one produces the "open with" dialog where the user can choose a tool from the existing list of them if any or else specify another tool.
I'm not aware of anything provided to let you retrieve the existing "open as" tool list though. You'd have to go registry spelunking for that as far as I know, and now you're into crazy territory anyway.
Both of the examples above open relative to the current directory, but in the second one we must provide the full path.
This stuff is covered in your October 2001 MSDN documentation CDs.
Thanks for your help.
I have tested your suggestions and both work for me.
However, there are two little problems:
1. I have coded my test for the first method as follows:
Code:
Private Sub Command105_Click()
Dim My_File_Path As String
My_File_Path = """" & "C:\temp2\Test1.txt" & """"
CreateObject("Shell.Application").ShellExecute My_File_Path, , , "open", SW_SHOWNORMAL
End Sub
And it works perfectly for me.
However, I have specified the full path of the file to open and it works.
You said that the path should be relative.
I don't understand then why it still works when I use the full path (absolute path).
Can you please clarify?
2. I have coded my test for the second method as follows:
Code:
Private Sub Command106_Click()
Dim My_File_Path As String
Const ssfDESKTOP = 0
My_File_Path = """" & "C:\temp2\Test1.txt" & """"
With CreateObject("Shell.Application").NameSpace(ssfDESKTOP)
With .ParseName(My_File_Path) '<-- Full path of file.
.InvokeVerb "openas"
End With
End With
End Sub
It doesn't work.
It gives me an error 91: Object variable or With Block variable not set
But if i pass the file path without those quotes like this:
Code:
Private Sub Command106_Click()
Dim My_File_Path As String
Const ssfDESKTOP = 0
My_File_Path = "C:\temp2\Test1.txt"
With CreateObject("Shell.Application").NameSpace(ssfDESKTOP)
With .ParseName(My_File_Path) '<-- Full path of file.
.InvokeVerb "openas"
End With
End With
End Sub
Then it works.
But why can I not add those double-quotes?
Isn't it better to enclose the file path in double-quotes?
Please advise.
Thanks.
-
May 18th, 2019, 06:28 PM
#7
Re: How to programmically open a file by an application
If you're interested in a more robust solution for #2, see my project about Association Handlers;
[VB6] List/Execute File Handlers: IAssocHandler and IAssocHandlerInvoker (Vista+)
-
May 19th, 2019, 01:34 PM
#8
Re: How to programmically open a file by an application
Originally Posted by IliaPreston
Isn't it better to enclose the file path in double-quotes?
Only on a command line you *have* to quote these filenames. You quote filenames in a string only if this string is going to be parsed by CommandLineToArgvW API function.
For instance when you use dir it's arguments are parsed by this API. Another example would be every command line utility written in C/C++ -- the runtime uses CommandLineToArgvW to parse the command line as it comes from the environment and pass the result in argc/argv pair to the executable's main function. So for instance curl.exe unknowingly uses the same API.
Every other API that accepts a filename as a parameter does *not* need quotes as there is nothing special about space in filenames outside of a command line, where space (along with tab and new line) is used as token delimiter.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|