|
-
Aug 4th, 2003, 02:06 PM
#1
Thread Starter
Banned
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
-
Aug 4th, 2003, 03:10 PM
#2
Frenzied Member
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:
Private Sub Command1_Click()
Dim strTemp
For i = 0 To List1.ListCount - 1
strTemp = strTemp & List1.List(i) & vbCrLf
Next i
With Text1
.Text = strTemp
.SelStart = 0
.SelLength = Len(.Text)
End With
SendKeys "^C"
Shell "notepad", vbNormalFocus
SendKeys "^V"
End Sub
-
Aug 4th, 2003, 03:44 PM
#3
Thread Starter
Banned
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:
Private Sub Command1_Click()
Dim strTemp
For i = 0 To List1.ListCount - 1
strTemp = strTemp & List1.List(i) & vbCrLf
Next i
With Text1
.Text = strTemp
.SelStart = 0
.SelLength = Len(.Text)
End With
SendKeys "^C"
Shell "notepad", vbNormalFocus
SendKeys "^V"
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
-
Aug 4th, 2003, 04:02 PM
#4
Thread Starter
Banned
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
-
Aug 4th, 2003, 04:47 PM
#5
Fanatic Member
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:
Private Sub Command1_Click()
Dim strTemp As String, i As Integer
For i = 0 To List1.ListCount - 1
strTemp = strTemp & List1.List(i) & vbCrLf
Next i
Clipboard.Clear
Clipboard.SetText strTemp
Shell "notepad", vbNormalFocus
SendKeys "^V"
End Sub
Author for Visual Basic Web Magazine
-
Aug 4th, 2003, 06:03 PM
#6
Fanatic Member
VB Code:
Dim FileNum As Integer, i As Long
FileNum = FreeFile
Open App.Path & "\temp.txt" For Output As #FileNum
For i = 0 To List1.ListCount - 1
Write #FileNum, List1.List(i)
Next
Close #FileNum
Shell "notepad """ & App.Path & "\temp.txt""", vbNormalFocus
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.
-
Aug 4th, 2003, 06:15 PM
#7
Fanatic Member
what? nobody's suggested SendMessage with the WM_SETTEXT message?
-
Aug 4th, 2003, 07:32 PM
#8
-
Aug 4th, 2003, 11:19 PM
#9
Fanatic Member
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.
-
Aug 4th, 2003, 11:39 PM
#10
Fanatic Member
yep... you'd need to add exactly three lines of needles code:
(this is conceptual, replace string literals as needed)
VB Code:
Do While hwndNotepad = 0 'Line 1
hwndNotepad = FindWindowEx(...) 'Line existed before I added the "wait for window" loop
DoEvents 'Line 2
Loop 'Line 3
-
Aug 5th, 2003, 06:18 PM
#11
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:
Do While hwndNotepad = 0 'Line 1
hwndNotepad = FindWindowEx(...) 'Line existed before I added the "wait for window" loop
DoEvents 'Line 2
Loop 'Line 3
If you make the loop like this :
VB Code:
Do
hwndNotepad = FindWindowEx(...)
DoEvents
Loop While hwndNotepad = 0
you won't need to have another line above
Has someone helped you? Then you can Rate their helpful post. 
-
Aug 5th, 2003, 11:12 PM
#12
Fanatic Member
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.
-
Aug 6th, 2003, 01:53 PM
#13
Fanatic Member
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.
-
Dec 26th, 2004, 01:26 PM
#14
Member
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:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Load()
Call MyShell
frmTip.Show
End Sub
Private Sub MyShell()
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
Dim Pid As Long
'Lock the window update
LockWindowUpdate GetDesktopWindow
'Execute notepad.Exe
Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
mWnd = InstanceToWnd(Pid)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on notepad
Putfocus mWnd
'Unlock windowupdate
LockWindowUpdate False
End Sub
Thank you!
-
Dec 26th, 2004, 02:22 PM
#15
Member
Re: To notepad...
Does anyone know how to do this?
-
Dec 26th, 2004, 02:24 PM
#16
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|