View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Macro for Creating Word Document!!

Is there a specific reason why you have to store the text in Word? A
simple text file (size) would be a lot smaller and require way less
performance overhead that automating Word...

Option Explicit

Sub ExportToTextFile()
Dim vData, n&, k&
Const sPath$ = "C:\Users\Akesh\Desktop\" '//edit to suit

k = Cells(Rows.Count, 1).End(xlUp).Row
vData = Range("$A$1:$B$" & k).Value
For n = LBound(vData) To UBound(vData)
WriteTextFileContents CStr(vData(n, 2)), sPath & vData(n, 1) &
".txt"
Next 'n
End Sub

Sub WriteTextFileContents(TextOut As String, _
Filename As String, _
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()

--
Garry

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