Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-07-2021, 05:26 AM
mc1903 mc1903 is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2016
Novice
Word VBA. How best to insert sections of other documents into a main document.
 
Join Date: Jan 2019
Posts: 8
mc1903 is on a distinguished road
Question Word VBA. How best to insert sections of other documents into a main document.

Hello,



I am looking for advice on the best approach to a challenge I have today.

I have a main Word document that I would like to insert sections of other Word documents (I'll call these documents 'snippets') into, to build out a boilerplate.

All documents (main & snippets) are built from the same template, so styles, etc are identical.

In each of my snippet documents, the section I wish to insert is currently bounded by simple text tags {start} & {end} each on their own otherwise blank pages. If there is a better way to identify the wanted section in each snippet doc, then I am open to changing from text tags.

I have some VBA working to give me a dialog box where I can select the multiple snippet documents and currently I am just outputting the full path of each file to Debug.Print. I am not sure how to process each snippet document, to locate the section between the tags, and insert it into the main doc.

Code:
Sub SelectSnippetFiles()
 
    Dim mfd As FileDialog
    Dim FileChosen As Integer
    Dim i As Integer
    Set mfd = Application.FileDialog(msoFileDialogFilePicker)
        mfd.InitialView = msoFileDialogViewList
        mfd.AllowMultiSelect = True
    FileChosen = mfd.Show
    
    If FileChosen = -1 Then
        For i = 1 To mfd.SelectedItems.Count
            Debug.Print mfd.SelectedItems(i)
            'I need something here to process the snippet sections :-/
        Next i
    End If

 End Sub
If anyone could give me a few pointers, I would be really grateful.

I am using Office 365, so have pretty much the latest version.

Thanks
M
Reply With Quote
  #2  
Old 10-07-2021, 08:47 AM
mc1903 mc1903 is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2019
Novice
Word VBA. How best to insert sections of other documents into a main document.
 
Join Date: Jan 2019
Posts: 8
mc1903 is on a distinguished road
Default

I have mashed two scripts together and have moved on a little bit. Is it kind of working, but its clunky.

I converted all of the simple text tags in each snippet document into Bookmarks. They are now called 'Start_of_Snippet' & 'End_of_Snippet' as {} braces are not allowed.

I added a bookmark in my main document also call 'End_of_Snippet' as it gets over written with each pass of the snippet file import.

Code:
Sub SelectSnippetFiles2()

    Dim mfd As FileDialog
    Dim FileChosen As Integer
    Dim i As Integer
    Set mfd = Application.FileDialog(msoFileDialogFilePicker)
        mfd.InitialView = msoFileDialogViewList
        mfd.AllowMultiSelect = True
    FileChosen = mfd.Show
    
    If FileChosen = -1 Then
        For i = 1 To mfd.SelectedItems.Count
            Debug.Print mfd.SelectedItems(i)
            
            Application.ScreenUpdating = False
            Dim DocSrc As Document, DocTgt As Document, RngSrc As Range, RngTgt As Range
            Set DocTgt = ActiveDocument
            Set DocSrc = Documents.Open(mfd.SelectedItems(i))
            Set RngTgt = DocTgt.Bookmarks("End_of_Snippet").Range
            With DocSrc
              Set RngSrc = .Range(.Bookmarks("Start_of_Snippet").Range.Start, .Bookmarks("End_of_Snippet").Range.End)
              RngTgt.FormattedText = RngSrc.FormattedText
              DocTgt.Bookmarks.Add "Destination", RngTgt
             .Close False
            End With
            Set RngSrc = Nothing: Set RngTgt = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
            Application.ScreenUpdating = True

        Next i
    End If

 End Sub
Follow up questions.
  1. Is there a way not to copy the bookmarks from the source (snippets) documents? I just want every thing between the bookmarks.
  2. Is there a way to set a sort order for the multiple file selection? I am thinking there would need to be a interim sort step between the file selection and the copy. Is there a standard sort dialog method?
Again, if anyone could suggest changes/improvements or just offer some kind thoughts and prayers; I would be really grateful.


Thanks
M
Reply With Quote
  #3  
Old 10-07-2021, 03:22 PM
Guessed's Avatar
Guessed Guessed is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

I don't like using bookmarks to transfer content or data. Looking at your code for instance, you keep moving the End_of_Snippet so you wouldn't be able to run the macro a second time without removing the additions the first time.

I tend to use Content Controls instead and find them a bit more flexible. For instance, if you have multiple snippets to transfer from each target doc then you could use matching tag and title properties of CCs in both source and target docs to marry content.
Code:
Dim aCC as ContentControl, aCC2 as ContentControl, sTag as String, sTitle as String
  For Each aCC In aDocSrc.SelectContentControlsByTag(sTag)
    sTitle = aCC.Title
    For Each aCC2 In aDocTgt.SelectContentControlsByTitle(sTitle)
      aCC2.Range.FormattedText = aCC.Range.FormattedText  'or add to end if you prefer
    Next
  Next aCC
Sorting the files is a bit involved. You could either save the list to an array (then use a bubblesort function to order) or to a recordset and sort that. I suspect the mfd.SelectedItems is already an array so that makes it probably the most likely contender.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 10-08-2021, 04:03 AM
mc1903 mc1903 is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2019
Novice
Word VBA. How best to insert sections of other documents into a main document.
 
Join Date: Jan 2019
Posts: 8
mc1903 is on a distinguished road
Default

Hello @Guessed

Thank you for your suggestions.

I am not (yet) familiar with Tags or ContentControls, so it's not immediately clear to me how I would implement this.

Very cheeky ask; would you have time modify the 2 documents attached, so I could see an example of how you would do it?

NOTE: The MasterTemplate.dotm does not have any VBA code in it; its just been saved as an empty macro enabled template.

Thank you again.
M
Attached Files
File Type: dotm MasterTemplate.dotm (14.8 KB, 5 views)
File Type: docx SnippetDoc.docx (14.7 KB, 6 views)
Reply With Quote
  #5  
Old 10-10-2021, 03:42 AM
Guessed's Avatar
Guessed Guessed is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

This is the macro and sample docs which align with it.
Code:
Sub SelectSnippetFiles3()
  Dim dlgFD As FileDialog, FileChosen As Integer, i As Integer
  Dim DocSrc As Document, DocTgt As Document, sTitle As String, rngTgt As Range, rngSrc As Range
  Dim ccTgt As ContentControl, ccSrc As ContentControl, ccsSrc As ContentControls
  
  Set dlgFD = Application.FileDialog(msoFileDialogFilePicker)
  With dlgFD
    .InitialView = msoFileDialogViewList
    .AllowMultiSelect = True
    FileChosen = .Show
  
    If FileChosen = -1 Then
      Set DocTgt = ActiveDocument
      For Each ccTgt In DocTgt.SelectContentControlsByTag("Snip")
        ccTgt.Range.Text = ""      'empty CC before refilling
      Next ccTgt
      'Application.ScreenUpdating = False
      For i = 1 To dlgFD.SelectedItems.Count
        Debug.Print dlgFD.SelectedItems(i)
        Set DocSrc = Documents.Open(dlgFD.SelectedItems(i))
        For Each ccSrc In DocSrc.SelectContentControlsByTag("Snip")
          sTitle = ccSrc.Title
          Set rngSrc = ccSrc.Range
          rngSrc.Select
          For Each ccTgt In DocTgt.SelectContentControlsByTitle(sTitle)
            Set rngTgt = ccTgt.Range
            If Not ccTgt.ShowingPlaceholderText Then rngTgt.Collapse Direction:=wdCollapseEnd
            rngTgt.FormattedText = rngSrc.FormattedText
          Next ccTgt
        Next ccSrc
        DocSrc.Close SaveChanges:=False
      Next i
      'Application.ScreenUpdating = True
    End If
  End With
 End Sub
Attached Files
File Type: dotm MasterTemplate.dotm (31.1 KB, 17 views)
File Type: docx SnippetDoc.docx (22.0 KB, 11 views)
File Type: docx SnippetDoc2.docx (21.5 KB, 11 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 10-11-2021, 01:18 AM
mc1903 mc1903 is offline Word VBA. How best to insert sections of other documents into a main document. Windows 10 Word VBA. How best to insert sections of other documents into a main document. Office 2019
Novice
Word VBA. How best to insert sections of other documents into a main document.
 
Join Date: Jan 2019
Posts: 8
mc1903 is on a distinguished road
Default

Morning @Guessed,

Amazing! Thank you so much for doing that. It is really appreciated!

Seeing content controls & tags in the example makes it much clearer to me now and I can see it is a far better way to implement what I am looking to achieve.

Thank you again for taking the time to do this.

All the best.
M

Last edited by mc1903; 10-11-2021 at 01:19 AM. Reason: Removed double line spacing.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA. How best to insert sections of other documents into a main document. insert multiple word docs into main word doc MimiCush Word 4 03-26-2018 01:07 PM
How to insert a section in the middle of a Word document, but keep the sections around it continuous vspofford Word 3 01-30-2016 01:19 PM
Can Word highlight the same text in the Reviewing Pane as in the main document? wordistheword Word 4 09-09-2013 04:50 AM
How to insert full documents into existing word document Laraak Word 1 03-07-2013 11:59 PM
Connecting documents to main document themangoagent Word 1 08-07-2009 10:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:26 AM.


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