Results 1 to 16 of 16

Thread: To notepad...

  1. #1

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    To notepad...

    I have 3 list boxes..I would like to send the contents of the listbox to a text file but I do not want to save this file through code..I just want notepad's instance to open up with the contents when its all done.

    For instance in one list box i have:

    lstBlah
    ---------
    100
    200
    300
    400
    etc...

    So I want to write to the text file:
    100
    200
    300
    400
    etc..

    and upon completion have this file open..which then allows me to save

    Can anyone help me out here..
    Thanks,
    Jon

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    It's maybe not a best way to do it, but easiest for sure:
    Place TextBox (Text1) on the form, set Visible=False;
    ListBox (List1), button (Command1)
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strTemp
    3.     For i = 0 To List1.ListCount - 1
    4.         strTemp = strTemp & List1.List(i) & vbCrLf
    5.     Next i
    6.    
    7.     With Text1
    8.         .Text = strTemp
    9.         .SelStart = 0
    10.         .SelLength = Len(.Text)
    11.     End With
    12.    
    13.     SendKeys "^C"
    14.     Shell "notepad", vbNormalFocus
    15.     SendKeys "^V"
    16. End Sub

  3. #3

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by andreys
    It's maybe not a best way to do it, but easiest for sure:
    Place TextBox (Text1) on the form, set Visible=False;
    ListBox (List1), button (Command1)
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strTemp
    3.     For i = 0 To List1.ListCount - 1
    4.         strTemp = strTemp & List1.List(i) & vbCrLf
    5.     Next i
    6.    
    7.     With Text1
    8.         .Text = strTemp
    9.         .SelStart = 0
    10.         .SelLength = Len(.Text)
    11.     End With
    12.    
    13.     SendKeys "^C"
    14.     Shell "notepad", vbNormalFocus
    15.     SendKeys "^V"
    16. End Sub
    This does not work correctly..
    it opens a notepad file with the contents

    Done:
    Exit Sub

    Err_Handler:
    Resume Done

    Are you sure we dont need to do anything with the text box? What is it used for???

    Jon

  4. #4

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Anyone else with some ideas...
    this should be fairly simple..I just want to list out the items from my listbox to a text file and have it open to that text file

    Jon

  5. #5
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Andreys' code simply copies the text to the clipboard, then opens notepad and pastes it. This code works, you also don't need a textbox anymore:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strTemp As String, i As Integer
    3.     For i = 0 To List1.ListCount - 1
    4.         strTemp = strTemp & List1.List(i) & vbCrLf
    5.     Next i
    6.    
    7.     Clipboard.Clear
    8.     Clipboard.SetText strTemp
    9.     Shell "notepad", vbNormalFocus
    10.     SendKeys "^V"
    11. End Sub
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  6. #6
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    VB Code:
    1. Dim FileNum As Integer, i As Long
    2.     FileNum = FreeFile
    3.     Open App.Path & "\temp.txt" For Output As #FileNum
    4.         For i = 0 To List1.ListCount - 1
    5.             Write #FileNum, List1.List(i)
    6.         Next
    7.     Close #FileNum
    8.     Shell "notepad """ & App.Path & "\temp.txt""", vbNormalFocus
    9.     Kill App.Path & "\temp.txt"

    This code is more robust than sendkeys
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  7. #7
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    what? nobody's suggested SendMessage with the WM_SETTEXT message?

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    FindWindow API to get the hwnd of notepad, FindWindowEx to find the "edit" of the notepad window and then the SetWindowText to put text...


    Has someone helped you? Then you can Rate their helpful post.

  9. #9
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    then you'd need to add needless code to wait for the notepad window to pop up, cuz what if it wasn't instanteous? (unless shell isn't asynchronous, in which case the harder way works fine too)
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  10. #10
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    yep... you'd need to add exactly three lines of needles code:

    (this is conceptual, replace string literals as needed)

    VB Code:
    1. Do While hwndNotepad = 0 'Line 1
    2.     hwndNotepad = FindWindowEx(...) 'Line existed before I added the "wait for window" loop
    3.     DoEvents 'Line 2
    4. Loop 'Line 3

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by agent
    yep... you'd need to add exactly three lines of needles code:

    (this is conceptual, replace string literals as needed)

    VB Code:
    1. Do While hwndNotepad = 0 'Line 1
    2.     hwndNotepad = FindWindowEx(...) 'Line existed before I added the "wait for window" loop
    3.     DoEvents 'Line 2
    4. Loop 'Line 3
    If you make the loop like this :

    VB Code:
    1. Do
    2.     hwndNotepad = FindWindowEx(...)
    3.     DoEvents
    4. Loop While hwndNotepad = 0

    you won't need to have another line above


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    in this particular case, it doesn't matter because in vb, variables are always initialized to zero. both of our examples would return the same result, but i'll admit, when running, my code wastes one more computer cycle than it should. by checking the variable at the top of the loop, it garantees that the first time the variable is checked, it will always be zero, whereas with your code, the check occures after setting the variable to the return value of FindWindowEx and the first check of the variable might return nonzero.

  13. #13
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    and i thought i was being geeky
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  14. #14
    Member
    Join Date
    Dec 2004
    Posts
    34

    Exclamation Re: To notepad...

    Sorry to bring up a really old topic, but is there a way to send the text to an embeded notepad window via a dialogue box? I mean, if the user entered text into text fields, and that data was then sent to the embeded notepad window. Then, you have another dialogue box, and then be able to send more information to the same embeded notepad window? Like, this is my code for the embeded notepad part:

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    2. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    3. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    6. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    7. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    8. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    9. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    10. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    11. Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    12. Const GW_HWNDNEXT = 2
    13. Dim mWnd As Long
    14.  
    15. Function InstanceToWnd(ByVal target_pid As Long) As Long
    16.     Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    17.     'Find the first window
    18.     test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    19.     Do While test_hwnd <> 0
    20.         'Check if the window isn't a child
    21.         If GetParent(test_hwnd) = 0 Then
    22.             'Get the window's thread
    23.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
    24.             If test_pid = target_pid Then
    25.                 InstanceToWnd = test_hwnd
    26.                 Exit Do
    27.             End If
    28.         End If
    29.         'retrieve the next window
    30.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    31.     Loop
    32. End Function
    33.  
    34. Private Sub Form_Load()
    35.     Call MyShell
    36.     frmTip.Show
    37. End Sub
    38.  
    39. Private Sub MyShell()
    40.     'KPD-Team 1999
    41.     'URL: [url]http://www.allapi.net/[/url]
    42.     'E-Mail: [email][email protected][/email]
    43.     Dim Pid As Long
    44.     'Lock the window update
    45.     LockWindowUpdate GetDesktopWindow
    46.     'Execute notepad.Exe
    47.     Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    48.     If Pid = 0 Then MsgBox "Error starting the app"
    49.     'retrieve the handle of the window
    50.     mWnd = InstanceToWnd(Pid)
    51.     'Set the notepad's parent
    52.     SetParent mWnd, Me.hwnd
    53.     'Put the focus on notepad
    54.     Putfocus mWnd
    55.     'Unlock windowupdate
    56.     LockWindowUpdate False
    57. End Sub

    Thank you!

  15. #15
    Member
    Join Date
    Dec 2004
    Posts
    34

    Re: To notepad...

    Does anyone know how to do this?

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: To notepad...

    It would be better to create a new thread.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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