Hi,
I was working on a word file. In the file I need to combine multiple word files from a dynamic location (means the location can changed as per the requirement).
I got a static code, which is working. But i want to convert it to a dynamic code, where i can set the folder location using a folder picker.
Please help.
Static Code
Code:
Sub MergeDocs()
Dim rng As Range
Dim MainDoc As Document
Dim strFile As String
Set MainDoc = ActiveDocument
Const strFolder = "C:\Users\A020105\Desktop\Test\" 'change to suit
strFile = Dir$(strFolder & "*.docx") ' can change to .docx
Do Until strFile = ""
Set rng = MainDoc.Range
rng.Collapse wdCollapseEnd
rng.InsertBreak
rng.InsertFile strFolder & strFile
strFile = Dir$()
Loop
ActiveDocument.TablesOfContents(1).Update
End Sub
Dynamic Code
Code:
Private Sub Document_New()
'
'
Dim rng As Range
Dim MainDoc As Document
Dim strFile As Variant
Dim strFolder As FileDialog
Set MainDoc = ActiveDocument
Set strFolder = Application.FileDialog(msoFileDialogFolderPicker)
strFolder.AllowMultiSelect = True
strFolder.Show
strFile = Dir$(STFolder & "\" & "*.docx")
Do While strFile <> ""
Set rng = MainDoc.Range
rng.Collapse wdCollapseEnd
rng.InsertBreak
rng.InsertFile STFolder & strFile
strFile = Dir$()
Loop
ActiveDocument.TablesOfContents(1).Update
End Sub