Excel to text file

T

Tony

I want to export data from several cells into the text file and I am
epxeriencing problem with formatting, I can not have line breaks to give me
new line for each entry. I have tried to use Chr(10) and Chr(13) but it does
not work. What is wrong in code below:

=====================================
Sub PrintAttachments()

Dim AttachmentsList As String

CheckSecurity

AttachmentsList = ""

Application.ScreenUpdating = False

For i = 1 To 10
For j = 3 To 12
If Worksheets(i).Cells(j, 17) <> "" Then
AttachmentsList = AttachmentsList + Worksheets(i).Cells(j,
17) & Chr(10)
End If
Next j
Next i
If AttachmentsList <> "" Then
AttachmentsList = "List of attachments: " & Chr(10) & Chr(10) &
AttachmentsList
Open "c:\temp\attachments.txt" For Output As #1
Write #1, AttachmentsList
Close #1
End If

Application.ScreenUpdating = True

End Sub
=====================================

Is there any way of printing my results instead of saving them into txt file ?

Thanks for help.

Tony
 
J

Joel

First you shouldn't use write, instead use print. Write add double quotes to
the data while print doesn't both have the same syntax.

Second, write one line at a time. Print will add the carriage return and
line feed

Open "c:\temp\attachments.txt" For Output As #1

For i = 1 To 10
For j = 3 To 12
If Worksheets(i).Cells(j, 17) <> "" Then
AttachmentsList = "List of attachments: " &
Worksheets(i).Cells(j, 17)
Write #1, AttachmentsList
End If
Next j
Next i
Close #1
End If
 
T

Tony

Hi Joel,

Thank you.

Tony

Joel said:
First you shouldn't use write, instead use print. Write add double quotes to
the data while print doesn't both have the same syntax.

Second, write one line at a time. Print will add the carriage return and
line feed

Open "c:\temp\attachments.txt" For Output As #1

For i = 1 To 10
For j = 3 To 12
If Worksheets(i).Cells(j, 17) <> "" Then
AttachmentsList = "List of attachments: " &
Worksheets(i).Cells(j, 17)
Write #1, AttachmentsList
End If
Next j
Next i
Close #1
End If
 
T

Tony

Hi Joel,

How I can have it without quotation marks ???
Sample output file looks like:

=======================
"List of Attachments :"


"Attachments 1"
"Attachments 2"
=======================

Thank you.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top