View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Writing to Notepad from VBA

Both writing a text file and opening that text file are quite simple:

To write to text:

Sub StringToTextFile(ByVal txtFile As String, _
ByVal strString As String)

Dim hFile As Long

hFile = FreeFile

Open txtFile For Output As hFile
'Write #hFile, strString 'this will make start- and end quotes
Print #hFile, strString;
Close #hFile

End Sub


To open the text file:

Sub OpenAsText(strPath As String)

Dim lRetVal As Long 'can do without this

On Error GoTo CANTOPEN

If bFileExists(strPath) Then
lRetVal = Shell("write" & " " & """" & strPath & """", 1)
Else
MsgBox "There is no valid file path supplied", , "opening text file"
End If

Exit Sub
CANTOPEN:

MsgBox "Could not open" & _
vbCrLf & vbCrLf & _
strPath, , "opening text file"

End Sub

Function bFileExists(ByVal sFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next
lAttr = GetAttr(sFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)
On Error GoTo 0

End Function


RBS


"stuart" wrote in message
...
Hi,

I'm writing a macro using VBA in Excel and to get the output from the
macro
to be written in Notepad rather than an Excel worksheet. How do I do this,
if
it's at all possible?

Thanks.