Results 1 to 30 of 30

Thread: Shell to Notepad

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Shell to Notepad

    Hi, I'm rewriting a project after losing the main code module.

    I had a routine that would shell to Notepad and display a text message.

    All the examples I Google refer to a specific file to Open Notepad with. Mine
    did not (from memory). It could be Saved if required, of course, but normally it would
    just be closed and never be seen again.

    Can anyone tell me how I could do this again ? (How did I do it in the first place, yes - asked myself that !!!)

    Thanks, ABB

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Shell to Notepad

    You could have a look at :

    Shell
    ShellExecute
    WinExec

    There is just one thing I do not understand. You mention that this is not a file being opened in Notepad, but as you describe I think it is indeed a file
    VB.NET MVP 2008 - Present

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Shell to Notepad

    Here's a simple, although somewhat unreliable, approach which does not mess with the Clipboard:

    Code:
    Private Declare Sub Sleep Lib "kernel32.dll" (Optional ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
        Shell "notepad", vbNormalFocus
        Sleep 100&
        SendKeys Text1.Text, True
    End Sub
    If you want a more robust method, you'll likely have to use APIs such as FindWindowEx and WM_SETTEXT.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    Hannes, I thought it might be too, but I have the original exe (with lost source) but
    cannot find a file written anywhere. And Notepad shows no source folder.

    Bonnie, maybe a did use Sendkeys as I'm no expert with APIs. How unreliable is unreliable ?

    Thanks for the responses !

    Regards, ABB

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Shell to Notepad

    Quote Originally Posted by AlexanderBB View Post
    How unreliable is unreliable ?
    Well, if the timing isn't right (Sleep 100&), the keystrokes might accidentally be sent to another application. In fact, it happened during my testing!

    With the API method, you can be 99% sure that Notepad will receive the text that you send to it.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    > With the API method, you can be 99% sure that Notepad will receive the text that you send to it.

    How complex ? Would you have an example ?

    Thanks, ABB

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Shell to Notepad

    Quote Originally Posted by AlexanderBB View Post
    Would you have an example ?
    Before I give you one, please describe first your complete requirements. For example, what exactly do you want to happen? How do you want it to be done? What are the inputs and what output do you expect? Etc...
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell to Notepad

    Sounds to me like a textbox and a few lines of code would do the trick without the need to shell to notepad.
    Am I missing something?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    The shell to notepad is to save -keep if required- even if you Exit VB. It was in the one I lost and hoping
    to replace the same as. I've forgotten how this bit was done though !

    Bonnie I can't think how to answer what you're asking - inputs outputs etc. It is just text in Notepad.
    Exactly like:
    Code:
    ff1 = FreeFile
    Open cc$ For Output As #ff1
    Print #ff1, theString
    Close #ff1
    ff = Shell("NOTEPAD.EXE " & cc$, 1)
    Except that writes a file to disk, then Opens it it Notepad. The old way didn't write any file, Notepad somehow opened with the text already there.
    It's doesn't really matter now... I can stick with the method above. Just be nice to know how it could be done.
    Last edited by AlexanderBB; Nov 4th, 2013 at 07:05 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell to Notepad

    Probably used the clipboard

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Shell to Notepad

    Quote Originally Posted by AlexanderBB View Post
    Except that writes a file to disk, then Opens it it Notepad. The old way didn't write any file, Notepad somehow opened with the text already there.
    It's doesn't really matter now... I can stick with the method above.
    If you are going to write temporary files to disk, it would be best to create them in the user's Temp folder (Environ$("Temp")). Call the GetTempFileName function to generate a unique file name.

    Quote Originally Posted by AlexanderBB View Post
    Just be nice to know how it could be done.
    My approach would have been to FindWindow (or EnumWindows) the shelled Notepad's window and check if its Process ID is equal to Shell's return value. If they match, I'd FindWindowEx the child Edit control and WM_SETTEXT the desired text.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    Thanks Bonnie, I may try your approach just to see if I succeed !!
    Agree on the temp file... I'm still kind of bugged I can't remember how I avoided that before. I tried Sendkeys and you can see each line
    appearing quite slowly. That didn't happen before so Sendkeys wasn't used... and not sure what datamiser means by clipboard...

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell to Notepad

    just save the file in temp or whereever appropriate, show it in notepad, delete file if not required to save

    sendkeys may well fail on windows7 (permission denied error)
    Last edited by westconn1; Nov 5th, 2013 at 06:01 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell to Notepad

    Quote Originally Posted by AlexanderBB View Post
    Thanks Bonnie, I may try your approach just to see if I succeed !!
    Agree on the temp file... I'm still kind of bugged I can't remember how I avoided that before. I tried Sendkeys and you can see each line
    appearing quite slowly. That didn't happen before so Sendkeys wasn't used... and not sure what datamiser means by clipboard...
    I mean that maybe it was copied to the clip board and then pasted in notepad

    I don't understand a need for notepad though, you could write the routines to display and save the file within your program similar to notepad in just a few minutes and IMO would be a better choice.

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    Maybe because Notepad has editing features that OP doesn't want to write


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Shell to Notepad

    @ABB,

    Maybe the code in post #35 here will give you some idea of how to send the data back to notepad.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    Quote Originally Posted by Nightwalker83 View Post
    @ABB,

    Maybe the code in post #35 here will give you some idea of how to send the data back to notepad.
    Do you mean post #3 and Sendkeys? I dunno... the mention of unreliabilty and problems with Win 7 don't inspire.... and the "Sleep" command didn't seem to exist in
    my Vb6. I'd like to try the API approach but every part of it is new and unknown. FindWindow (or EnumWindows) the shelled Notepad's window and check if its Process ID is equal to Shell's return value. If they match, I'd FindWindowEx the child Edit control and WM_SETTEXT the desired text. . Doubt if I'd make it halfway through that!

  18. #18
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Shell to Notepad

    Quote Originally Posted by AlexanderBB View Post
    Do you mean post #3 and Sendkeys? I dunno... the mention of unreliabilty and problems with Win 7 don't inspire.... and the "Sleep" command didn't seem to exist in my Vb6. I'd like to try the API approach but every part of it is new and unknown. FindWindow (or EnumWindows) the shelled Notepad's window and check if its Process ID is equal to Shell's return value. If they match, I'd FindWindowEx the child Edit control and WM_SETTEXT the desired text. . Doubt if I'd make it halfway through that!
    No! If you run the code I linked to you will see that it displays the result in notepad. I am guessing you want to do the same thing with you code?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell to Notepad

    and the "Sleep" command didn't seem to exist in
    my Vb6. I'd like to try the API approach
    sleep is yet another API
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Shell to Notepad

    Sorry Nightwalker, I didn't notice that link in the message. After having a look I think weeding out the relative parts
    are a bit hard for me. I need to get it finished and the write-file method works. And APIs seem far too murky.

    Thanks to everyone for their help and advice.

    Regards, ABB

  21. #21
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell to Notepad

    Quote Originally Posted by jmsrickland View Post
    Maybe because Notepad has editing features that OP doesn't want to write
    It does have find and replace but that is about it, not very hard to write code for that.

    Notepad is a very simple program, a textbox, a menu and a common dialog with just a little code would be able to replicate it and could be a useful exercise for novice programmers.

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    It also has Undo, Cut, Copy, Paste, Delete, Select All, not to mention Format options

    Given that I didn't want to write those functionalities I would Shell Notepad and then get it's handle and SendMessageByString
    Last edited by jmsrickland; Nov 6th, 2013 at 12:17 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  23. #23
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Shell to Notepad

    Quote Originally Posted by jmsrickland View Post
    It also has Undo, Cut, Copy, Paste, Delete, Select All, not to mention Format options

    Given that I didn't want to write those functionalities I would Shell Notepad and then get it's handle and SendMessageByString
    Most of those are extremely simple to write [1 or 2 lines of code] and/or are already supported via shortcut keys.

    The format options? Really, they consist of allowing you to change the font and turn word wrap on or off, both are very simple to do with a text box.

    That said the OP said that he was shelling to notepad just to allow them to save the text so I would imagine that non of those are even needed.

    My point was that rather than try to shell to notepad and loading a file or getting into API calls to transfer data it would be simple to just use a form with a text box and write a few lines of code behind it and you have the added benefit of the program being self contained and not requiring Notepad to be available on the target system.

  24. #24
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    I was actually speaking for myself what I would do on post #22 providing I didn't want to write those functions. Also, you don't have to worry about always having Notepad to be available if you went one step further and loaded Notepad.exe in a Resource file. But that's a totally different subject.

    And your point is taken. For nothing more than just saving the text I would also just put a textbox on a Form with a Save button


    EDIT: Please disregard the bold statement above. It is not a good idea. I was wrong to even state that.
    Last edited by jmsrickland; Nov 6th, 2013 at 05:55 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  25. #25
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Shell to Notepad

    Quote Originally Posted by jmsrickland View Post
    Also, you don't have to worry about always having Notepad to be available if you went one step further and loaded Notepad.exe in a Resource file. But that's a totally different subject.
    I am fascinated that this extremely poor practice comes up so often in threads here. Notepad also is not safe to plop and run on different Windows versions since it makes use of different Edit Control features on different Windows versions, i.e. the code is tailored to the OS.

    This is almost as shabby as window spelunking and hijacking to try to automate programs that are not meant to be automated. It will not be long before antivirus software begins flagging any programs calling FindWindow due to the rampant abuse. UIPI is not granular enough to do the job alone.

    Programmers should start making more use of windowless controls to prevent such hijacking of your own software. The big boys are doing this more and more in self defense.

  26. #26
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    You're absolutely right, dilettante, I should never had posted that statement about putting Notepad.exe in a Resource file. I will amend my post.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  27. #27
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Shell to Notepad

    Why would you need to include notepad with your program anyway? It IS a part of the Windows operating system and if anyone needs to uses Notepad with their program they can just use a free one such as Notepad ++, etc.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  28. #28
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    You wouldn't


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  29. #29
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Shell to Notepad

    Quote Originally Posted by jmsrickland View Post
    You wouldn't
    Why mention putting Notepad in a resource file in post #24?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  30. #30
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Shell to Notepad

    I didn't, you're just imagining that


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width