Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Send worksheet to text file


Hi all,

How to write codes or to send one worksheet to textfile.txt ?
or in plain word to save as textfile.txt outside excel itself.

Thanks in advance.


--
mtlpp
------------------------------------------------------------------------
mtlpp's Profile: http://www.excelforum.com/member.php...o&userid=23348
View this thread: http://www.excelforum.com/showthread...hreadid=376426

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Send worksheet to text file

Try if this suits:


Sub RangeToText()

Dim arr
Dim varDialogResult
Dim strFile As String
Dim strFileName As String

strFileName = Replace(ActiveWorkbook.Name, ".xls", ".txt", 1, -1,
vbTextCompare)

varDialogResult = _
Application.GetSaveAsFilename(InitialFileName:=str FileName, _
FileFilter:="Text Files (*.txt), *.txt")

'to take care of a cancelled dialog
'----------------------------------
If varDialogResult = False Then
Exit Sub
Else
strFile = varDialogResult
End If

If bFileExists(strFile) Then
If MsgBox(strFile & _
vbCrLf & vbCrLf & _
"Already exists, overwrite this file?", _
vbExclamation + vbYesNo + vbDefaultButton2, _
"save range to text file") = vbNo Then
Exit Sub
End If
End If

arr = ActiveWindow.RangeSelection

SaveArrayToText strFile, arr

End Sub


Sub SaveArrayToText(ByVal txtFile As String, _
ByRef arr As Variant, _
Optional ByVal LBRow As Long = -1, _
Optional ByVal UBRow As Long = -1, _
Optional ByVal LBCol As Long = -1, _
Optional ByVal UBCol As Long = -1, _
Optional ByRef fieldArr As Variant)

'this one organises the text file like
'a table by inserting the right line breaks
'------------------------------------------
Dim R As Long
Dim C As Long
Dim hFile As Long

If LBRow = -1 Then
LBRow = LBound(arr, 1)
End If

If UBRow = -1 Then
UBRow = UBound(arr, 1)
End If

If LBCol = -1 Then
LBCol = LBound(arr, 2)
End If

If UBCol = -1 Then
UBCol = UBound(arr, 2)
End If

hFile = FreeFile

Open txtFile For Output As hFile

If IsMissing(fieldArr) Then
For R = LBRow To UBRow
For C = LBCol To UBCol
If C = UBCol Then
Write #hFile, arr(R, C)
Else
Write #hFile, arr(R, C);
End If
Next
Next
Else
For C = LBCol To UBCol
If C = UBCol Then
Write #hFile, fieldArr(C)
Else
Write #hFile, fieldArr(C);
End If
Next
For R = LBRow To UBRow
For C = LBCol To UBCol
If C = UBCol Then
Write #hFile, arr(R, C)
Else
Write #hFile, arr(R, C);
End If
Next
Next
End If

Close #hFile

End Sub


RBS


"mtlpp" wrote in
message ...

Hi all,

How to write codes or to send one worksheet to textfile.txt ?
or in plain word to save as textfile.txt outside excel itself.

Thanks in advance.


--
mtlpp
------------------------------------------------------------------------
mtlpp's Profile:
http://www.excelforum.com/member.php...o&userid=23348
View this thread: http://www.excelforum.com/showthread...hreadid=376426


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 do I only send 1 worksheet of a file in an e-mail? JeffL Excel Worksheet Functions 1 April 15th 09 05:27 PM
SAVE & SEND A FILE BASED ON TEXT IN A CELL VIA MACRO Pat Baratta New Users to Excel 1 February 26th 07 06:46 AM
unable to send excel file as attachment tab greyed out in file com Wayne Excel Discussion (Misc queries) 0 April 13th 06 10:06 PM
send contents of cell b4 to a plain text file lothario[_68_] Excel Programming 5 January 22nd 04 09:37 AM
How do I send some cells to an external text file? lothario[_2_] Excel Programming 1 October 12th 03 02:34 PM


All times are GMT +1. The time now is 02:39 PM.

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

About Us

"It's about Microsoft Excel"