PDA

View Full Version : Solved: Spacing problems when combining multiple docs into one.



DonCard
06-30-2006, 01:49 PM
I have a macro that combines several single page documents into one document. The problem I am having is, when combined, the top sentence or two of the second page will intrude on the first page which throws off the whole document. I can correct the spacing with the final document but it is a pain. How do I keep each single page document on its own page when combining them?

lucas
06-30-2006, 02:15 PM
search the kb.....
http://vbaexpress.com/kb/getarticle.php?kb_id=214

fumei
06-30-2006, 07:41 PM
A rather elaborate piece for something that (may be) quite easy to solve.

You didn't post code, so I am making assumtions.

Are you opening each file and getting the contents, building it that way? Or are you using InsertFile? In any case, a simple possible solution is just to add a page break between the single page files.

You have two choices. Which one you use depends on the structure of the document. Selection.InsertBreak Type:=wdPageBreak
' OR....
Selection.InsertBreak Type:=wdSectionBreakNextPage

DonCard
07-01-2006, 05:15 PM
Here is the code I use (supplied by VBAX) to combine the single page docs into one document.


Sub Document_New()
Dim wrd As Word.Application
Set wrd = CreateObject("word.application")
wrd.Visible = True
AppActivate wrd.Name
fname = Dir("C:\Documents and Settings\.....\MultiDocs\*.doc")
Do While (fname <> "")
With wrd
Selection.EndKey Unit:=wdStory
Selection.InsertFile FileName:=("C:\Documents and Settings\.....\MultiDocs\" & fname)
End With
fname = Dir
Loop
Set wd = Nothing
End Sub

I tried the code suggested by Lucas but it caused a worse spacing problem than that above.

I did try placing a page break at the end of each individual document prior to combining but that did not help the spacing problem.

Does the above code lends itself to the Selection.InsertBreak tag?

fumei
07-01-2006, 08:17 PM
1. WHY on earth are you making an instance of Word, sicne you are already IN Word - you are using Document_New. Document_New fires when a template creates a new document. So...you are IN Word. Why are you doingDim wrd As Word.Application
Set wrd = CreateObject("word.application")?

2. Please use the VBA tags in your posts. This puts your code in a code window, rather than as text in the post.

3. I hope you are using Option Explicit. It does not appear that you are, because fname is never declared.

4. Yes.Sub Document_New()
Dim fPath As String
Dim fname
fPath = "C:\Documents and Settings\blah\MultiDocs\"
fname = Dir(fpath& "*.doc")
Do While fname <> ""
With Selection
.EndKey Unit:=wdStory
.InsertBreak Type:=wdPageBreak
.InsertFile FileName:= (fpath & fname)
End With
fname = Dir
Loop
End Sub

lucas
07-02-2006, 08:07 AM
Hi DonCard,
vba tags added to your post, hope you don't mind.

DonCard
07-02-2006, 10:23 AM
Thanks Lucas!

In trying not to confuse things I did just the opposit...sorry. The code is only part of the macro I am using (the whole macro is over 300 lines). I included the Sub Document_New().....Sub statements to show how the macro itself begins and ends.

Since I am a code user and not a creator, I am not able to answer Fumei's questions but the following may help explain what I am up to http://vbaexpress.com/forum/showthread.php?t=8342 "AutoOpen Problems" .

I did try using Fumei's code in my doc but it hung up on: fname = Dir(fpath& "*.doc") .

I tried adding Selection.InsertBreak Type:=wdPageBreak into my (VBAX) code and it solved the spacing problem but added a blank page between each page of the final document. When substituting Selection.InsertBreak Type:=wdSectionBreakNextPage, It also solved the spacing problem, but added a blank page between about half of the printed pages. My only problem now is how to get rid of the blank pages.

fumei
07-02-2006, 01:15 PM
I did try using Fumei's code in my doc but it hung up on: fname = Dir(fpath& "*.doc") . what does hung up mean? If you get error messages, you have to tell us what they say.

Did you make your own corrections to fPath?

OK, I suspect that there is information in SOME of your section breaks, but not others. Section breaks contain information.

Can you post a file with your full code? Perhaps some dummy files to work with?

DonCard
07-03-2006, 12:08 PM
Hung up means the macro stopped. It was my error, I made a mistake in typing in the fPath. The macro works just fine, better than what I had before.

The problem is not with macros but with the spacing in the original merge documents. By adjusting the spacing of each original merge document (twelve in all) then running the merge and combine macros to see how each change affected the final document, I was finally able to get the spacing correct.

Thanks for the help.