Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Text Files with Widow Control

Status
Not open for further replies.

rhpen

Programmer
Nov 2, 2005
40
US
In my Word 2003 macro I am inserting a number of text files into a Word document. Simple. See following code, but I would like to not have a text file break at the Word page break (the text files can have variable number of lines). I just can't figure out how to do it. If I concatenate the text file into a paragraph, the text file line formatting is off. If I use the text file as is, each line is treated as a paragraph by Word. Any thoughts? Thanks

For i = 1 To nf
Selection.EndKey Unit:=wdStory
Selection.InsertFile FileName:="c:\test\file$(i)
Selection.TypeParagraph
Selection.TypeParagraph
Next
 





How about inserting into a temp doc, and then, replacing the ^p paragraph with nothing.

Copy the result and paste into your doc.

Delete the temp doc.

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
I would like to not have a text file break at the Word page break "

Not following that. text file break????

"If I concatenate the text file into a paragraph, the text file line formatting is off. If I use the text file as is, each line is treated as a paragraph by Word."

Do you not want each line to remain independent? That means each is a paragraph.

Not following what you are looking for.

Selection.InsertFile FileName:="c:\test\file$(i)

That filename looks odd to me. Surely there must be a closing ", yes?

You may want to consider using Range, rather than Selection.
Code:
Dim r As Range
Set r = ActiveDocument.Range
   For i = 1 To nf
      With r
         .Collapse Direction:=wdCollapseEnd
         .InsertFile FileName:="c:\test\file$(i)"
         .Collapse Direction:=wdCollapseEnd
         .InsertParagraphAfter
         .InsertParagraphAfter
      End With
   Next
Of course the Range could easily be a new document, and not even the active one.

However, I am still not clear on whether you want the line breaks, or not. And what breaks are between the text files? I see nothing doing that.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks Fumei,

You are exactly right about the file name. A terrible blunder on my part. It should, of course, be

Selection.InsertFile FileName:="c:\test\"+file$(i)

It is correct in my actual code, but was not in my post.

I do want each line of the text files to be independent (they don't line up correctly otherwise). I also want to keep all the lines of each text file together in the Word document. I do not want some of the lines of a text file on one page of the Word document and the rest of the lines of the same text file on the next page of the Word document.
If I just use "insert file" as in my code, Word simply breaks them up at the Word page breaks.

I am now working on a method of inserting all the files, letting Word break them up as it will, then going from the top down on the Word document and using "keep with next" on all paragraphs except the blank one right before each file. It looks promising, but I'm still figuring out how to tell when I get to the 2nd of the blank lines between each file.

I hope this has explained more clearly what I am after and any other suggestions you might have would be much appreciated.

Thanks,
 
If I just use "insert file" as in my code, Word simply breaks them up at the Word page breaks.
Of course it does. Why would it not?

" the 2nd of the blank lines between each file."

THAT tells me you are not using styles. Using Styles would help.....a lot.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top