Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2021, 04:26 AM
Trader174 Trader174 is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2016
Novice
Combining Selection of Documents
 
Join Date: Jul 2021
Posts: 4
Trader174 is on a distinguished road
Default 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
Any help would be gratefully received.
Reply With Quote
  #2  
Old 07-10-2021, 08:30 PM
gmayor's Avatar
gmayor gmayor is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 07-11-2021, 02:31 AM
Trader174 Trader174 is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2016
Novice
Combining Selection of Documents
 
Join Date: Jul 2021
Posts: 4
Trader174 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 07-11-2021, 05:56 AM
gmayor's Avatar
gmayor gmayor is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 07-11-2021, 03:36 PM
Guessed's Avatar
Guessed Guessed is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #6  
Old 07-22-2021, 11:07 AM
Trader174 Trader174 is offline Combining Selection of Documents Windows 10 Combining Selection of Documents Office 2016
Novice
Combining Selection of Documents
 
Join Date: Jul 2021
Posts: 4
Trader174 is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining Selection of Documents 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 Selection of Documents Combining documents of different formats Blaie Word 1 06-04-2011 06:35 PM
Combining Selection of Documents Combining Word Documents dms997 Word 5 02-26-2011 03:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft