#1
|
|||
|
|||
Combining Selection of Documents
Hi Everyone
I am new to VBA coding. I have created a word template using content controls for users to fill out with come conditional rules and it works good. I want to be able to create a userform with a selection of document templates that can be selected to be merged into the current document. The only difference in formatting is sometimes the additional page is in portrait and sometimes in landscape. I am taking baby steps here so my first goal is to see if I can get VBA to append a selected file to the current document. All the code examples I have found (such as stickied on this forum) seems to only to be for a folder of documents. What do I need to do to be able to choose a selection of files within a folder, rather than the whole folder? Here is an example of one I tried: Code:
Sub MergeDocs() Dim rng As Range Dim MainDoc As Document Dim strFile As String, strFolder As String Dim Count As Long With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Pick folder" .AllowMultiSelect = False If .Show Then strFolder = .SelectedItems(1) & Application.PathSeparator Else Exit Sub End If End With Set MainDoc = Documents.Add strFile = Dir$(strFolder & "*.doc") ' can change to .docx Count = 0 Do Until strFile = "" Count = Count + 1 Set rng = MainDoc.Range With rng .Collapse wdCollapseEnd If Count > 1 Then .InsertBreak wdSectionBreakNextPage .End = MainDoc.Range.End .Collapse wdCollapseEnd End If .InsertFile strFolder & strFile End With strFile = Dir$() Loop MsgBox ("Files are merged") lbl_Exit: Exit Sub End Sub |
#2
|
||||
|
||||
Use the filepicker instead (below) - or see Insert a selection of documents
Code:
Sub MergeDocs() Dim rng As Range Dim MainDoc As Document Dim strFile As String Dim Count As Long With Application.FileDialog(msoFileDialogFilePicker) .Title = "Pick files" .AllowMultiSelect = True If .Show <> -1 Then Exit Sub Set MainDoc = Documents.Add For Count = 1 To .SelectedItems.Count strFile = .SelectedItems(Count) Set rng = MainDoc.Range With rng .Collapse wdCollapseEnd If Count > 1 Then .InsertBreak wdSectionBreakNextPage .End = MainDoc.Range.End .Collapse wdCollapseEnd End If .InsertFile strFile End With Next Count End With MsgBox ("Files are merged") lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi Graham
The code changes you kindly provided does do what I am looking for. However, I am wanting to insert into the current open document rather than a new. Is this possible? Also the document I am trying to insert is in landscape orientation yet the previous page is portrait. The document ends up as portrait. Can this code be modified to preserve orientation? Many thanks |
#4
|
||||
|
||||
See your other thread at Is this possible?.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
||||
|
||||
Looking at this and the other thread, I am wondering why you aren't doing this by using building blocks in a single template. If you saved the 'other docs' as quick parts then you can insert them easily from that dropdown and not need code or multiple files.
If page setup is part of the required insertion then you need to simply include section breaks in the building block.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Sorry for the delay in reponding.
Thanks for the tip. I didn't know about this feature. It could work pretty well. I will give it a go. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
combining word documents | jesselscott | Word | 1 | 04-28-2015 09:55 AM |
Combining Word Documents with Comments | kimmers41 | Word | 0 | 05-06-2014 12:56 PM |
Combining/merging documents | hawkeyefxr | Word | 11 | 08-03-2012 03:01 AM |
Combining documents of different formats | Blaie | Word | 1 | 06-04-2011 06:35 PM |
Combining Word Documents | dms997 | Word | 5 | 02-26-2011 03:25 AM |