Macro to Join Files

G

Gary Cassidy

I have a folder with nearly 100 documents that I want joined into one.
I cobbled together the following code based on several past posts I
found but I have not been able to successfully run it. The best has
been about 8 documents that get inserted before the macro crashes with a
message stating "Code execution has been interupted" When I choose
"debug", the line which inserts a page break is highlighted.

Any ideas?



Sub JoinFiles()
Dim xFile As String
Documents.Add
' Get files one at a time.
xFile = Dir$("c:\ACMDP2\*.doc")
Do While xFile <> ""
' Insert first file
Selection.InsertFile FileName:="c:\ACMDP2\" & xFile
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.Collapse Direction:=wdCollapseEnd
' Get next file
xFile = Dir$()
Loop

End Sub

Thanks

Gary
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Gary Cassidy > écrivait :
In this message, < Gary Cassidy > wrote:

|| I have a folder with nearly 100 documents that I want joined into one.
|| I cobbled together the following code based on several past posts I
|| found but I have not been able to successfully run it. The best has
|| been about 8 documents that get inserted before the macro crashes with a
|| message stating "Code execution has been interupted" When I choose
|| "debug", the line which inserts a page break is highlighted.
||
|| Any ideas?
||

Not sure, but try with a range object instead of a selection object, which
can be flaky...

Play around with:

'_______________________________________
Sub JoinFiles()
Dim xFile As String
Dim xPath As String
Dim MyRange As Range

xPath = "C:\ACMDP2\"

Documents.Add
' Get files one at a time.

xFile = Dir$(xPath & "*.doc")
Do While xFile <> ""
' Insert first file
Set MyRange = ActiveDocument.Range
With MyRange
.Collapse wdCollapseEnd
.InsertFile xPath & xFile
.End = ActiveDocument.Range.End
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
End With
' Get next file
xFile = Dir$()
Loop

Set MyRange = Nothing

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Gary Cassidy

Jean-Guy,

Perfect! The solution you sent worked right out of the cut & paste!

Merci infiniment!

Gary
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Gary Cassidy > écrivait :
In this message, < Gary Cassidy > wrote:

|| Jean-Guy,
||
|| Perfect! The solution you sent worked right out of the cut & paste!
||
|| Merci infiniment!
||
|| Gary

De rien!
Glad I could help.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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