PDA

View Full Version : Need info to combine documents



Pineapple
05-26-2004, 04:29 PM
Need a macro that will combine a large group of recipes in one folder into a new blank document. So I can go through and apply Heading 1 style to all the recipe names, generate a table of contents, and then see your recipes, and go to outline view and move them around.

Anne Troy
05-26-2004, 05:00 PM
Hi, Pineapple! Sounds familiar! Give me a few minutes, or perhaps someone else will reply before me.

Glad to see you could make it!

For the record, we're assuming that you have a bunch of recipes in different Word documents, and that they're all in a folder by themselves, right? No extra files in that folder? Or...you can put them all into one folder, right?

Anne Troy
05-26-2004, 05:15 PM
Okay, here we go.


Sub RunMe()
Dim doc As Document
Dim num_files As Integer
Dim file_name As String
Dim dir_path As String
Dim file_ext As String

Set doc = Application.Documents.Add

dir_path = "C:\Documents and Settings\Your Name\My Documents\"
file_ext = "doc"

file_name = Dir(dir_path & "*." & file_ext)
Do While Len(file_name) > 0
With Selection
.InsertFile dir_path & file_name
.EndKey Unit:=wdStory
.InsertBreak Type:=wdPageBreak
End With
file_name = Dir()
Loop
End Sub


Instructions:

Open a blank document.
Copy the code above.
Go back to Word and hit Alt+F11 to bring up the Visual Basic Editor (VBE).
Double-click ThisDocument on the left side of the page, then paste the code into the right side of the page.

Change the path in the code above to match the one that contains all your recipes. If you're not sure, just go right-click one of your filenames and hit Properties. You'll see the path in there, and it will probably be very similar to the one in the code, especially if you're running Windows XP.

Then close the VBE using the X on the window.

With that document in front of you now, hit Tools-Macro-Macros, and double-click "RunMe'. Within a moment you should have all your recipes in one file.

Save your file. If you like, you can now hit Alt+F11 again, and delete all the code; save your file again, and close the VBE.

Go through each recipe. When you find the title for each one, click somewhere in the middle of it, and choose Heading 1 from the styles dropdown.

Let's see if we can get you that far.

Good luck!

PS: This code written by BCF (http://www.vbaexpress.com/forum/member.php?u=16), not me!

Pineapple
05-26-2004, 06:39 PM
when i try to run macro, i get the message that macros are not enabled. how do i enable the macros?

Zack Barresse
05-26-2004, 06:49 PM
go to Tools -> Macros -> Security. set to Medium. when you open a document that will prompt you to Enable or Disable macros. you have the option of LOW but that will not give you an option. Medium is recommended.

hth

Anne Troy
05-27-2004, 04:27 PM
Any luck, Pineapple?
:)

Anne Troy
06-09-2004, 01:39 PM
Any luck here?

espencer
05-04-2005, 08:23 AM
Hello,
I'm new to this thread. This works GREAT. I would like to know if in anyway when documents are added that they keep the original layout. I have different layouts in the folder. Some of the documents are portrait and others are landscape. Any ideas or suggestion on combining them in one document?

MOS MASTER
05-05-2005, 07:33 AM
Hello,
I'm new to this thread. This works GREAT. I would like to know if in anyway when documents are added that they keep the original layout. I have different layouts in the folder. Some of the documents are portrait and others are landscape. Any ideas or suggestion on combining them in one document?
Hi and welcome to VBAX! :hi:

What you want is possible but there can be many Cave-at's on the way! (And they all have to do with sections and document formatting)

I think this will get you on your way:
Sub CombineDocuments()
Dim oApp As New Word.Application
Dim oTarget As Word.Document: Dim oInsert As Word.Document
Dim sFile As String: Dim sPath As String: Dim sExt As String
Dim iOrient As Integer
Set oTarget = oApp.Documents.Add

sPath = "C:\Documents and Settings\Admin\Desktop\Test\"
sExt = "doc"
sFile = Dir(sPath & "*." & sExt)

Do While Len(sFile) > 0
Set oInsert = oApp.Documents.Open(FileName:=sPath & sFile)
iOrient = oInsert.PageSetup.Orientation
oInsert.Close False
Set oInsert = Nothing

With oApp.Selection
If iOrient = 0 Then
.PageSetup.Orientation = wdOrientPortrait
Else
.PageSetup.Orientation = wdOrientLandscape
End If

.InsertFile FileName:=sPath & sFile
.Collapse Direction:=wdCollapseEnd

sFile = Dir()
If Len(sFile) > 0 Then
.InsertBreak Type:=wdSectionBreakNextPage
End If
End With
Loop

With oApp
.Visible = True
.Activate
End With

Set oTarget = Nothing
Set oApp = Nothing
End Sub

This Sub will run a lot slower because we have to open each document we like to insert to retrieve the page orientation! (So we know how to deal with sections page setup)

There are probably better ways of doing this but this is a good starting point!

Enjoy! :whistle:

espencer
05-05-2005, 10:21 AM
This seems to work but like you said I'll need to address the sections and document formatting of the documents. Thanks for the help. If you could, would you point me in some direction on sections and document formatting. I would appreciate it.
Again THANKS.

MOS MASTER
05-05-2005, 10:27 AM
This seems to work but like you said I'll need to address the sections and document formatting of the documents. Thanks for the help. If you could, would you point me in some direction on sections and document formatting. I would appreciate it.
Again THANKS.
Hi, :D

You're Welcome!

I've done quick tests with this code and everything was merged in a neat fashion without losing formatting.

I couldn't say exactly wich cave-at's you will encounter exactally.

If you come about pertical problems than you can place them over here (Clearly explained) so we can try to catch the cause of this problem.

My code places each document into it's own section in Word and that normally should be enough to ensure formatting integrity!

Enjoy! :whistle:

Anne Troy
05-05-2005, 10:29 AM
When you run Joost's code, what is NOT kept?

fumei
05-05-2005, 10:45 AM
Hi Joost. Interesting, in that this is the WORD Help forum, that you have the code creating a new Word application. Care to comment why?

In any case, here is an alternative that adjusts Anne's original code. It is true, unfortunately, that to maintain file orientation you have to open it first. But you CAN maintain it by using a Section break, rather than Anne's original PageBreak. I also notice you open the file, check the orientation, CLOSE the file, then insert the file.

If the file is open, you may as well just copy it..... then close it.

Dim doc As Document
Dim r As Range
Dim num_files As Integer
Dim file_name As String
Dim dir_path As String
Dim file_ext As String
Dim lngPage As Long

Set doc = Application.Documents.Add

dir_path = "C:\Documents and Settings\gerry.knight\My Documents\gerry\"
file_ext = "doc"

file_name = Dir(dir_path & "*." & file_ext)
Do While Len(file_name) > 0
Documents.Open FileName:=dir_path & file_name
lngPage = ActiveDocument.PageSetup.Orientation
Set r = Selection.Range
r.WholeStory
r.Copy
ActiveDocument.Close wdDoNotSaveChanges
With Selection
With .Sections(1).PageSetup
.Orientation = lngPage
.SectionStart = wdSectionNewPage
End With
.PasteAndFormat wdFormatOriginalFormatting
.InsertBreak Type:=wdSectionBreakNextPage
.EndKey unit:=wdStory
End With
file_name = Dir()
Loop

MOS MASTER
05-05-2005, 11:00 AM
Hi Joost. Interesting, in that this is the WORD Help forum, that you have the code creating a new Word application. Care to comment why?

Hi Gerry, :D

Well the main reason was to keep everything invisible to the eye off the one who's executing the macro.

Word has a habbit off screen flickering when opening a lot off documents...(That screenupdating = false don't always fixxes)

If that was the right way to go...you tell me?

Like I said there are better ways of coding this...the best in my opion is the one that runs reliable and fast...(Will test you're code wich is shorter so if it's faster...then it's better) :rofl:



In any case, here is an alternative that adjusts Anne's original code. It is true, unfortunately, that to maintain file orientation you have to open it first.

That's what I hate about the code the most as I mentioned...It slow's things down to much..



But you CAN maintain it by using a Section break, rather than Anne's original PageBreak. I also notice you open the file, check the orientation, CLOSE the file, then insert the file.

If the file is open, you may as well just copy it..... then close it.

Like we said before you have to read the orientation so on that we agree.

I don't like copy/Paste (Cause I believe I had problems in the past with that because it relies on the default paste options in higher version off Word..That is if I recall well) :eek:
So basically I don't like copy/paste in code that was my reason for doing so...

The other thing was that the Insertfile command is so fast that it doesn't matter in speed.
Ah now I run your code I see you forgot to test if there is a new file to insert like I do in the code. This leaves you with a blank page to many on the end of your code!

Like:
sFile = Dir()
If Len(sFile) > 0 Then
.InsertBreak Type:=wdSectionBreakNextPage
End If

fumei
05-05-2005, 11:18 AM
Yes, I forgot to test. At the end of the Dir, move the Selection to the end of the document, check if it is a Chr(12), if so, delete. Actually, the way the code is, it always WILL be a Chr(12), so really no need to test, just delete it.

Alternatively, add logic that if the return from Dir has a Len(filename) = 0 delete the Selection.

MOS MASTER
05-05-2005, 11:30 AM
Hi Gerry, :D

Yes you could delete it..it doesn't seam to make any difference for me.

As for logics we all have our own for me it's more sensible to test if there are more files if so add a break if not don't...:yes

To me there could be one improvent to the code and that's to check for trailing Chr$(13) after you've inserted the documents and before we insert a break..(To tiddy things up)

espencer
05-05-2005, 12:18 PM
Thanks for all of the help so far. I've enclosed the three documents in questioned. Note02 is in portrait, note03 is in portrait and note04 is in landscape. I think the problem is in the formatting of the documents. Feel free to run the notes through the macro and give me you take on this. THANKS.

MOS MASTER
05-05-2005, 12:34 PM
Hi, :D

Did you try Gerry's Sub?

It run's faster then mine and has a better result then my sub! :cloud9:
I've altered it a little bit so that the last blank page is no longer there

Here it is by Dreamboat/Gerry/and little old me:
Sub test()
Dim doc As Document
Dim r As Range
Dim num_files As Integer
Dim file_name As String
Dim dir_path As String
Dim file_ext As String
Dim lngPage As Long

Application.ScreenUpdating = False
Set doc = Application.Documents.Add

dir_path = "C:\Documents and Settings\Admin\Desktop\Test\"
file_ext = "doc"

file_name = Dir(dir_path & "*." & file_ext)
Do While Len(file_name) > 0
Documents.Open FileName:=dir_path & file_name
lngPage = ActiveDocument.PageSetup.Orientation
Set r = Selection.Range
r.WholeStory
r.Copy
ActiveDocument.Close wdDoNotSaveChanges
With Selection
With .Sections(1).PageSetup
.Orientation = lngPage
.SectionStart = wdSectionNewPage
End With
.PasteAndFormat wdFormatOriginalFormatting
file_name = Dir()
If Len(file_name) > 0 Then
.InsertBreak Type:=wdSectionBreakNextPage
End If
.EndKey unit:=wdStory
End With
Loop

End Sub

Have a try just change the path of dir_path :whistle:

fumei
05-05-2005, 01:11 PM
I tried it with your test files. Seems to work fine for me. Are you having problems?

MOS MASTER
05-06-2005, 03:02 AM
Thanks for all of the help so far. I've enclosed the three documents in questioned. Note02 is in portrait, note03 is in portrait and note04 is in landscape. I think the problem is in the formatting of the documents. Feel free to run the notes through the macro and give me you take on this. THANKS.
Hi, :D

Did you have time to try the code in my previous post? (Seams to work well for me and Gerry)

:whistle:

espencer
05-09-2005, 11:12 AM
Thanks for all of the help and advice. I've learned alot from this. I had to reformat the word document and everything works GREAT. Thanks again.

MOS MASTER
05-09-2005, 11:17 AM
Hi, :D
Glad you made it work...you're welcome! :beerchug:
(Personally loved what Gerry's made of it!)