PDA

View Full Version : [SOLVED:] Get selected documents from user's choice and merge all documents as a single doc



Ramdoss
07-25-2015, 10:08 AM
We have 15 word docx files stored in c:\docs folder named "a1.docx, a2.docx, a3.docx...a15.dox " I have created a word file to get user's choice through 15 checkboxes displayed (with file name). I have named the bookmark of each checkbox as "a1, a2, a3...a15". The user may select any 2 or more documents from the list displayed with ticking the check box.

Can anybody help with a macro to merge selected documents only in the current document itself?

Searching the net, got sample vba macro code to merge all the documents in a particular folder as a single file. I want to merge only selected documents from the list.

gmayor
07-25-2015, 08:53 PM
See http://www.gmayor.com/Boiler.htm

Ramdoss
07-25-2015, 11:32 PM
gmayor thanks a lot - your boiler template is a huge collection of batch process. I want to use your vba macro found, for selecting 8 files (sample) :


Sub YaddaMultiLine()Dim myFiles() As String
Dim myFilesListed As String
Dim j As Long
Dim var

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.ButtonName = "OK"
.Title = "Select Parent Folder"
If .Show = 0 Then Exit Sub
If .SelectedItems.Count > 1 Then
For var = 1 To .SelectedItems.Count
ReDim Preserve myFiles(j)
myFiles(j) = .SelectedItems(var)
j = j + 1
Next
Else
myFiles(0) = .SelectedItems(1)
End If
If j <> 9 Then
MsgBox "HEY!!! You are supposed to select 8 files." & _
vbCrLf & "Macro will terminate. Try again."
Exit Sub
End If
End With
If j = 0 Then
myFilesListed = myFiles(0)
Else
For var = 0 To UBound(myFiles())
myFilesListed = myFilesListed & myFiles(var) & _
vbCrLf
Next
End If
MsgBox myFilesListed
End Sub

Instead of asking the user to select files from displaying the files list in a directory, I want to store the selected files from his input and merge only those documents. Here is the screenshot of my simple user form.

14003
Pl help.

gmayor
07-26-2015, 12:37 AM
Assuming your useform has default names for the various elements then you could use the following code. Note that it uses one of your documents as a template:


Option Explicit

Sub Macro1()
Const strPath As String = "C:\Docs\"
Dim i As Long
Dim oDoc As Document
Dim orng As Range
Dim frmFiles As New UserForm1
With frmFiles
.Caption = "Select Documents"
.Show
If .Tag = 0 Then GoTo lbl_Exit
Set oDoc = Documents.Add(strPath & "a1.docx")
Set orng = oDoc.Range
orng.Text = ""
If .CheckBox1.Value = True Then
orng.InsertFile strPath & "a1.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox2.Value = True Then
orng.InsertFile strPath & "a2.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox3.Value = True Then
orng.InsertFile strPath & "a3.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox4.Value = True Then
orng.InsertFile strPath & "a4.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox5.Value = True Then
orng.InsertFile strPath & "a5.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox6.Value = True Then
orng.InsertFile strPath & "a6.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox7.Value = True Then
orng.InsertFile strPath & "a7.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox8.Value = True Then
orng.InsertFile strPath & "a8.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox9.Value = True Then
orng.InsertFile strPath & "a9.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox10.Value = True Then
orng.InsertFile strPath & "a10.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox11.Value = True Then
orng.InsertFile strPath & "a11.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox12.Value = True Then
orng.InsertFile strPath & "a12.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox13.Value = True Then
orng.InsertFile strPath & "a13.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox14.Value = True Then
orng.InsertFile strPath & "a14.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
If .CheckBox15.Value = True Then
orng.InsertFile strPath & "a15.docx"
orng.End = oDoc.Range.End
orng.Collapse 0
End If
End With
lbl_Exit:
Unload frmFiles
Set frmFiles = Nothing
Set oDoc = Nothing
Set orng = Nothing
Exit Sub
End Sub


The userform code (with two buttons - one for cancel) would be


Option Explicit

Private Sub CommandButton1_Click()
Me.Tag = 1
Me.Hide
End Sub

Private Sub UserForm_Click()
Me.Tag = 0
Me.Hide
End Sub

Ramdoss
07-26-2015, 08:22 AM
:):) Thanks a lot gmayor - this is what I was trying for the past two days - thanks again. I have added a page break after every file insert command in your macro. works fine.!