Save each text line as a different file?

T

Tony

I have a document with thousands of lines of text, separated by a
carriage return. Something like this:

this is an example
this is another example
this is yet another example
..../...
this is the last line of the example

I there a way to save each of such lines or to export each line as a
different text document?

With Word or any other application. Preferably on Mac, but I can also
run Windows on Parallels Desktop on Mac OS X 10.4.9.

Thanks.
 
J

JE McGimpsey

One simplistic way:

Public Sub ParasToTextFiles()
Const csPATH As String = "<your path here>:"
Const csFILENAME As String = "test"

Dim nFILENUM As Long
Dim i As Long
Dim sOut As String
Dim sFName As String

nFILENUM = FreeFile
With ActiveDocument.Paragraphs
If .Count > 0 Then
For i = 1 To .Count
sFName = csPATH & csFILENAME & Format(i, "0000.txt")
sOut = .Item(i).Range.Text
Open sFName For Output As nFILENUM
Print #nFILENUM, Left(sOut, Len(sOut) - 1)
Close #nFILENUM
Next i
End If
End With
End Sub
 
T

Tony

Thanks, but when I copy and paste it into Apple Script Editor, I get an
error when trying to execute it: AppleScript Error (null)" or when
trying to save it as Script or as Application: "Syntax Error. A
identifier can't to after this identifier", highlighting the "Public
Sub" string.

Any way to fix it? Thanks.

---------------
 
J

JE McGimpsey

Tony said:
Thanks, but when I copy and paste it into Apple Script Editor, I get an
error when trying to execute it: AppleScript Error (null)" or when
trying to save it as Script or as Application: "Syntax Error. A
identifier can't to after this identifier", highlighting the "Public
Sub" string.

Any way to fix it? Thanks.

Well, the first problem is that it's not an AppleScript - it's a VBA
Macro. You can get information on using macros here:

http://word.mvps.org/mac/installmacro.html

NOTE - I posted an entire macro, so the page above is WRONG about where
to paste it.

Follow the procedure, but once you are dumped into the VBE, delete all
the text in it before pasting the macro.

One could probably translate the VBA into an AppleScript routine.
MacTech has an outstanding resource for conversions written by MVP Paul
Berkowitz:

http://www.mactech.com/vba-transition-guide/index-001.html

I'd recommend the PDF version - it will be a lot easier to read.
 
T

Tony

Thanks.

I created the macro. How to save it? I just created it and closed the
macro editor by selecting "Word/Close and Return to Microsoft Word". I
could not see other way. No feedback or confirmation if that was right
or wrong or if the macro was indeed created or saved or not. Confusing
to me.

Now, the macro shows in "Tools/Macro/Macros", but when I open a
document to process with such Macro and select "Macro/Macros", select
such Macro and Run, I get an error in a Microsoft Visual Basic window:
"Run-time error '76'. Path not found". Then I select "End".

The macro that I have created is as you indicated (FULL TEXT, INCLUDING
ALL IN THE MACRO EDITOR WINDOW):

---
Public Sub ParasToTextFiles()
Const csPATH As String =
"/Users/tony/Desktop/MacroOutput/testfile.doc:"
Const csFILENAME As String = "test"

Dim nFILENUM As Long
Dim i As Long
Dim sOut As String
Dim sFName As String

nFILENUM = FreeFile
With ActiveDocument.Paragraphs
If .Count > 0 Then
For i = 1 To .Count
sFName = csPATH & csFILENAME & Format(i, "0000.txt")
sOut = .Item(i).Range.Text
Open sFName For Output As nFILENUM
Print #nFILENUM, Left(sOut, Len(sOut) - 1)
Close #nFILENUM
Next i
End If
End With
End Sub
---

First I just pasted the macro that you described including the line:

Const csPATH As String = "<your path here>:"

Since I got the error above, I changed the corresponding line for the
following one where the file to process lives:

Const csPATH As String =
"/Users/tony/Desktop/MacroOutput/testfile.doc:"

Yet, I get the same error. I am lost.

Any way to process the open Word file without indicating its path or
even saving it first? Any way for Word to ask-prompt for the user to
browse the hard disk and navigate to where the file lives in the hard
disk, to process it with the macro.

Sorry for all the questions.

Thanks.

-----------------
 
J

JE McGimpsey

Tony said:
Since I got the error above, I changed the corresponding line for the
following one where the file to process lives:

Const csPATH As String =
"/Users/tony/Desktop/MacroOutput/testfile.doc:"

Yet, I get the same error. I am lost.

Any way to process the open Word file without indicating its path or
even saving it first? Any way for Word to ask-prompt for the user to
browse the hard disk and navigate to where the file lives in the hard
disk, to process it with the macro.

Sorry for all the questions.

Don't be.

You have it mostly right - but

a) you need to include the HD name, and use MacOS path separators rather
than Unix:

Const csPATH As String = _
"Macintosh HD:Users:tony:Desktop:MacroOutput:"

b) change the filename constant in csFILENAME:

Const csFILENAME As String = "testfile"

c) I put the the extension, .txt rather than .doc, since the output
files are text files, not Word documents, in the

sFName = csPATH & csFILENAME & Format(i, "0000.txt")

line. If you really want them to have the .doc extension, change that
line to

sFName = csPATH & csFILENAME & Format(i, "0000.\doc")

If you followed the directions in the link I gave, you stored the macro
in the Normal template, which is a good place to store it, as it will be
available from any document.
 
T

Tony

Wow! Great! It works (almost). Thanks!!!

The only thing is that instead of being named "testfile0001.txt" and so
on, the files generated are named as "testfile0001,txt" and so on (note
the comma instead of the stop before "txt"). Oops! How to fix it?
Thanks.

The Macro now reads:

---
Public Sub ParasToTextFiles()
Const csPATH As String = "LaCie500:Users:gabriel:Desktop:MacroOutput:"
Const csFILENAME As String = "testfile"

Dim nFILENUM As Long
Dim i As Long
Dim sOut As String
Dim sFName As String

nFILENUM = FreeFile
With ActiveDocument.Paragraphs
If .Count > 0 Then
For i = 1 To .Count
sFName = csPATH & csFILENAME & Format(i, "0000.txt")
sOut = .Item(i).Range.Text
Open sFName For Output As nFILENUM
Print #nFILENUM, Left(sOut, Len(sOut) - 1)
Close #nFILENUM
Next i
End If
End With
End Sub
---

Thanks again for your great support!!!

:)

------------
 
J

JE McGimpsey

Works for me, but may depend on regional preferences.

Try

sFName = csPATH & csFILENAME & Format(i, "0000\.txt")
 
D

Daiya Mitchell

JE said:
You can get information on using macros here:

http://word.mvps.org/mac/installmacro.html

NOTE - I posted an entire macro, so the page above is WRONG about where
to paste it.

Just by the way--I did account for that in the text, also. :)
Common Problems: All macros must start with a Sub statement and end
with an End Sub. If the code someone gave you already had the Sub/End
Sub in it, you will need to delete the extra Sub lines.

So, only wrong, rather than WRONG. :)))

Daiya
 
J

JE McGimpsey

Daiya Mitchell said:
Just by the way--I did account for that in the text, also. :)


So, only wrong, rather than WRONG. :)))

Ah - I didn't read down to the "Common Problems" section. I simply saw
the declarative "You can paste the provided macro code right where the
cursor is." as WRONG <g>.
 
D

Daiya Mitchell

JE said:
Ah - I didn't read down to the "Common Problems" section. I simply saw
the declarative "You can paste the provided macro code right where the
cursor is." as WRONG <g>.

I'll try to remember to tweak it. I tend to assume that people who don't
know what they are doing such that they need the link will read the
whole page, but come to think of it--I don't think even I do that, when
I'm looking for web code hints. :)

Daiya
 
J

JE McGimpsey

Daiya Mitchell said:
I tend to assume that people who don't know what they are doing such
that they need the link will read the whole page

Hmmm...

That certainly wouldn't apply to me <g> - I tend to read just enough to
dig myself in deeper. Manuals are only to read after exhausting all
other options.

Nuclear reactor, torpedo, and ballistic missile operating manuals were
rare exceptions.
 

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