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
Any help would be gratefully received.