Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ShellExecute to open a file in a specific program - Possible?

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
A program I'm working on creates a .csv file as output. My boss has decided that it should be output in printable form (and the .csv isn't very pretty to look at in Notepad). Opening the file in Excel, or OpenOffice's Calc, would solve that problem and allow for nice printouts. However, on my Dev box the .csv file extension is associated with Notepad, and I don't know what associations that extension may have on the enduser's system. Is there some way I can force the file open in Excel or Calc, preferably without trying to change the extension associations?
 
Can you directly run the app (CreateProcess) with the file in question as parameter #1? I know you can do similar things with ShellExecute. But if you just provide the document URI, ShellExecute will use whatever is defined in the extension associations to bring the file up.

To me, the viable options seem to be:
1) Run the app directly using CreateProcess. Of course, this means determining and storing the app's path somehow (if it's constant as a constant in the program, if it's not, trying to search the drive to find it). Basically duplicating what ShellExecute does.
2) Change the extension associations. This may be preferable, since there would be a potential of less effort, and if the management preference for all things is for CSV to be brought up in something other than Notepad, the guys and gals that handle the OS configuration and such should get on that.
 
This works for me:
Code:
ShellExecute(Handle, 'open', 'C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE', 'c:\test.csv', nil, SW_SHOWNORMAL);
Obviously, it's not a good idea to hard code paths to applications so you'd need to elaborate on that.

Hope this helps!


Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Stretchwickster said:
Code:
ShellExecute(Handle, 'open', 'C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE', 'c:\test.csv', nil, SW_SHOWNORMAL);

Maybe I'm not too versed on the differences between ShellExecute and CreateProcess, but isn't this just the same as calling CreateProcess with the path to excel.exe and the CSV file as parm #1? I always thought ShellExecute was preferable when you used the features of the Windows shell (like in Explorer), like URI association so you could simply pass the URI and get the desired result, or directly print if the app supported it (like via right-click in Explorer).

When is one or the other preferable? I notice for just calling applications that CreateProcess seems to be a little better in terms of performance, control (the API is there to manipulate the process afterwards), and reliability.

Any thoughts on this?
 
AFAIK Shellexecute is the 'old' way of calling and external program.
There is rather more to using Createprocess safely, but the overall system is more flexible.
There are examples of both methods in the FAQ's.


Steve: N.M.N.F.
Playing the blues isn't about feeling better. It's about making other people feel worse.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top