Board index » delphi » Launch NotePad with textfile loaded

Launch NotePad with textfile loaded

Hello!

I want NotePad to be launched, from a button.Click, with a specific
textfile. I use
ShellExecute(Handle,nil,Pchar('NOTEPAD.EXE'),nil,nil,sw_SHOWNORMAL);
to launch it with, but where do I specify the path and filename of the
textfile to load?

--
Regards
Kai Inge

 

Re:Launch NotePad with textfile loaded


In article <01bdb955$3a1a4200$799f4382@IT-StepUser>, "Kai Inge Buseth"

Quote
<kibus...@online.no> writes:
>Hello!

>I want NotePad to be launched, from a button.Click, with a specific
>textfile. I use
>ShellExecute(Handle,nil,Pchar('NOTEPAD.EXE'),nil,nil,sw_SHOWNORMAL);
>to launch it with, but where do I specify the path and filename of the
>textfile to load?

Two possibilities.  First, in the quotes: 'Notepad.exe mypath\myfile.txt' (you
might have to double up the backslashes, but a little playing around will
settle that).

My way: instead of Notepad in the quotes, put the textfile in the quotes and
let Windows figure out what program to launch.  That way the user gets to view
it in a different word processor if he/she prefers.

/js

Re:Launch NotePad with textfile loaded


Quote
>>I want NotePad to be launched, from a button.Click, with a specific
>>textfile. I use
>>ShellExecute(Handle,nil,Pchar('NOTEPAD.EXE'),nil,nil,sw_SHOWNORMAL);
>>to launch it with, but where do I specify the path and filename of the
>>textfile to load?

>Two possibilities.  First, in the quotes: 'Notepad.exe mypath\myfile.txt' (you
>might have to double up the backslashes, but a little playing around will
>settle that).

>My way: instead of Notepad in the quotes, put the textfile in the quotes and
>let Windows figure out what program to launch.  That way the user gets to view
>it in a different word processor if he/she prefers.

While "your way" can be a good idea, the original poster wanter to know
how to pass a command line to *notepad*, and your first suggestion is
not the correct use of ShellExecute - the lpParameters parameter should
be used:

ShellExecute(Handle, nil, 'notepad.exe', 'c:\bootlog.txt', nil,
SW_SHOWNORMAL);

--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Launch NotePad with textfile loaded


In article <JxYL0BA03Jv1E...@jcollins.demon.co.uk>, Jeremy Collins

Quote
<j...@jcollins.demon.co.uk> writes:
>While "your way" can be a good idea, the original poster wanter to know
>how to pass a command line to *notepad*, and your first suggestion is
>not the correct use of ShellExecute - the lpParameters parameter should
>be used:

>ShellExecute(Handle, nil, 'notepad.exe', 'c:\bootlog.txt', nil,
>SW_SHOWNORMAL);

>--
>Jeremy Collins
>Kansai Business Systems

>(return ad

Oops.  Sorry.  Should have checked previously written code before responding.
/js

Re:Launch NotePad with textfile loaded


Hello again and thank you all for answering me. Both ways worked out, but
now I became curious on the method of letting Windows find out which app to
read the file. Just like when double click the file in explorer - the
corresponding app launches. But - if I use the suggested  code with a
different extention than .txt, say .Iwp for Lotus Word Pro to launch -
Nothing happens, (not even the Except Message) And if I force it open with
the following code - Word Pro gives a message CVJan.Iwp does not exist, do
you want to make one? I make one and when saving the file I get two files
with the same name in the directory!

Try
 ShellExecute(Handle,nil,'WordPro.exe','C:\Lotus\jobs\wordpro\CVJan.Iwp',nil
,sw_SHOWNORMAL);
Except
  MessageDlg('That one did not work!  ',
     mtInformation,[mbOk],0);
end;

If I double-click the file in explorer it launches Word Pro with the
correct file loaded.
What do I do wrong, when Im not able to do this by code?
--
Regards
Kai Inge

JefSummers <jefsumm...@aol.com> wrote in article
<1998072813525800.JAA23...@ladder03.news.aol.com>...

Quote
> In article <JxYL0BA03Jv1E...@jcollins.demon.co.uk>, Jeremy Collins
> <j...@jcollins.demon.co.uk> writes:

Re:Launch NotePad with textfile loaded


Kai,

Ok... Here you go...

To start a file by double-clicking it in Explorer, the extension has
to be in the Windows Registry, and associated with an application.
Windows uses this registry entry to find out what application it
should start, and then passes the filename on the command line to
the application, along with a switch telling the application what
to do with the file.

So, to open your Word Pro document, it's simply

ShellExecute(0, 'open', 'C:\Lotus\jobs\wordpro\CVJan.Iwp',
    nil, nil, SW_NORMAL);

To print the same file, just change it to:

ShellExecute(0, 'print', 'C:\Lotus\jobs\wordpro\CVJan.Iwp',
    nil, nil, SW_NORMAL);

It's really that simple... <g>

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Quote
Kai Inge Buseth wrote:

> Hello again and thank you all for answering me. Both ways worked out, but
> now I became curious on the method of letting Windows find out which app to
> read the file. Just like when double click the file in explorer - the
> corresponding app launches. But - if I use the suggested  code with a
> different extention than .txt, say .Iwp for Lotus Word Pro to launch -
> Nothing happens, (not even the Except Message) And if I force it open with
> the following code - Word Pro gives a message CVJan.Iwp does not exist, do
> you want to make one? I make one and when saving the file I get two files
> with the same name in the directory!

> Try
>  ShellExecute(Handle,nil,'WordPro.exe','C:\Lotus\jobs\wordpro\CVJan.Iwp',nil
> ,sw_SHOWNORMAL);
> Except
>   MessageDlg('That one did not work!  ',
>      mtInformation,[mbOk],0);
> end;

> If I double-click the file in explorer it launches Word Pro with the
> correct file loaded.
> What do I do wrong, when Im not able to do this by code?
> --
> Regards
> Kai Inge

> JefSummers <jefsumm...@aol.com> wrote in article
> <1998072813525800.JAA23...@ladder03.news.aol.com>...
> > In article <JxYL0BA03Jv1E...@jcollins.demon.co.uk>, Jeremy Collins
> > <j...@jcollins.demon.co.uk> writes:

Re:Launch NotePad with textfile loaded


In article <01bdba7c$46686640$179f4382@IT-StepUser>, Kai Inge Buseth
<kibus...@online.no> writes

Quote
>Hello again and thank you all for answering me. Both ways worked out, but
>now I became curious on the method of letting Windows find out which app to
>read the file. Just like when double click the file in explorer - the
>corresponding app launches. But - if I use the suggested  code with a
>different extention than .txt, say .Iwp for Lotus Word Pro to launch -
>Nothing happens, (not even the Except Message) And if I force it open with
>the following code - Word Pro gives a message CVJan.Iwp does not exist, do
>you want to make one? I make one and when saving the file I get two files
>with the same name in the directory!

>Try
> ShellExecute(Handle,nil,'WordPro.exe','C:\Lotus\jobs\wordpro\CVJan.Iwp',nil
>,sw_SHOWNORMAL);
>Except
>  MessageDlg('That one did not work!  ',
>     mtInformation,[mbOk],0);
>end;

>If I double-click the file in explorer it launches Word Pro with the
>correct file loaded.
>What do I do wrong, when Im not able to do this by code?

As the other poster suggested, if you just want to open a document
inside its default application, you do not need to specify the
application name:

ShellExecute(Handle, nil, 'C:\Lotus\jobs\wordpro\CVJan.Iwp', nil, nil,
  SW_SHOWNORMAL);

By the way, did you really mean to give your WordPro file an IWP
extension? Shouldn't it be LWP?

Oh, and ShellExecute failing will not usually cause a Delphi-style
exception. You need to check that the return value of the function
is greater than 32 for success.
--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Launch NotePad with textfile loaded


Hello again,

Thank you for the answers. This was an embarrassing error. I simply read
the lower case l for an upper case I. Therefore it failed. But I learn
something new for each error :)
Thanks again.
--
Regards
Kai Inge

Jeremy Collins <j...@jcollins.demon.co.uk> wrote in article
<clNOhAAjitv1E...@jcollins.demon.co.uk>...

Quote
> In article <01bdba7c$46686640$179f4382@IT-StepUser>, Kai Inge Buseth
> <kibus...@online.no> writes

> By the way, did you really mean to give your WordPro file an IWP
> extension? Shouldn't it be LWP?

> Oh, and ShellExecute failing will not usually cause a Delphi-style
> exception. You need to check that the return value of the function
> is greater than 32 for success.
> --
> Jeremy Collins
> Kansai Business Systems

> (return address not altered 'coz I get spammed *whatever* I do!)

Other Threads