Do Loop or For...Next?

J

Jason Logue

Hi, I currently have part of my code that separates an ascii text file
into multiple word docs by the paragraph marker. My code retrieves a
certain number in the file and saves the document with that number as
the name. There is one extra step I would like to add, but I am not
sure how to add it. I need the code to insert a file into the
beginning of each newly created document. This inserted file is going
to be the header file; each separate file will eventually be a data
source for a shell document. Would you anyone have any good
suggestions for this? Here is the code I have for the number grab,
file separation and saving of each file.

Dim fname As String, i As Integer, Source As Document, Target As
Document
Set Source = ActiveDocument
For i = 1 To Source.Paragraphs.Count
Set Target = Documents.Add

Target.Range = Source.Paragraphs(i).Range
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.MoveUntil Cset:="."
Selection.MoveUntil Cset:=","
Selection.MoveLeft Unit:=wdCharacter, Count:=12,
Extend:=wdExtend



fname = Selection
Target.SaveAs FileName:=fname & ".doc"
Target.Close
Set Target = Nothing

Next i

Set Source = Nothing


End Sub


TIA - Jason
 
J

Jonathan West

Hi Jason,

I've indicated below what code you need to insert and where.

Jason Logue said:
Hi, I currently have part of my code that separates an ascii text file
into multiple word docs by the paragraph marker. My code retrieves a
certain number in the file and saves the document with that number as
the name. There is one extra step I would like to add, but I am not
sure how to add it. I need the code to insert a file into the
beginning of each newly created document. This inserted file is going
to be the header file; each separate file will eventually be a data
source for a shell document. Would you anyone have any good
suggestions for this? Here is the code I have for the number grab,
file separation and saving of each file.

Dim fname As String, i As Integer, Source As Document, Target As
Document
Set Source = ActiveDocument
For i = 1 To Source.Paragraphs.Count
Set Target = Documents.Add

Target.Range = Source.Paragraphs(i).Range
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.MoveUntil Cset:="."
Selection.MoveUntil Cset:=","
Selection.MoveLeft Unit:=wdCharacter, Count:=12,
Extend:=wdExtend



fname = Selection

Insert some code here

Selection.Move Unit:=wdStory, Count:=-1
Selection.InsertFile FileName:="My header file.doc"
'use the full pathname of your header file in the line above
Target.SaveAs FileName:=fname & ".doc"
Target.Close
Set Target = Nothing

Next i

Set Source = Nothing


End Sub


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

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