Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default How open notepad.exe and write text line by line via excel vba?

Hi,
At the end of my code I need to open notepad.exe and write text line by line.
How to do with excel vba.
Please help. Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default How open notepad.exe and write text line by line via excel vba?

"geniusideas" wrote:
At the end of my code I need to open notepad.exe and
write text line by line. How to do with excel vba.


No need to run notepad.exe. See the following paradigm.

FYI: The macro writes the same line twice two different ways to demonstrate
the difference. I use the Print...myLine method in order to have complete
control over spacing.


Sub makeTextFile()
Const myDir = "C:\temp\"
Const myOutFile = "temp.txt"
Dim fnOut As Long
Dim n As Long, myLine As String, myOutPath As String

myOutPath = myDir & myOutFile

fnOut = FreeFile
Open myOutPath For Output Access Write As #fnOut

For n = 1 To 10
myLine = "Line " & n & Format(Timer(), " 0.000000")
Print #fnOut, myLine
Print #fnOut, "Line "; n; Format(Timer(), " 0.000000")
Next
n = n - 1

Close
MsgBox "done" & vbNewLine & n & " lines"
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default How open notepad.exe and write text line by line via excel vba?

Hi,
At the end of my code I need to open notepad.exe and write text line
by line. How to do with excel vba.
Please help. Thanks


Expanding on joeu2004's example in another way...

Writing large amounts of text to a file is slow at best, and so I offer
the following reusable procedure that will let you write any amount of
text to a file, OR append any amount of text to an existing file.

Assemble your entire file content into a string containing line returns
for each line of text, then *dump* the string into a file in one
shot...

Sub WriteTextFileContents(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends large amounts
' of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum As Integer
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFileContents()

...so, for example, if your text was in an array you could simply write
to file like this...

WriteTextFileContents Join(myArray, vbCrLf), myFilename

...where 'myFilename includes the full path.

To append more text to an existing file...

WriteTextFileContents Join(myArray, vbCrLf), myFilename, True

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How write into text file at first and last line with excel vba? geniusideas Excel Programming 3 March 22nd 12 05:36 PM
Shell open Notepad and write text vqthomf Excel Programming 1 December 14th 09 12:29 PM
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? No Name Excel Programming 6 October 7th 09 12:28 PM
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? No Name Excel Worksheet Functions 7 October 7th 09 11:10 AM
write a line from Excel to a text file julian_bro Excel Programming 1 September 30th 03 01:29 AM


All times are GMT +1. The time now is 01:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"