![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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
I am using Office 365, so have pretty much the latest version. Thanks M |
|
#2
|
|||
|
|||
|
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
Thanks M |
|
#3
|
||||
|
||||
|
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#6
|
|||
|
|||
|
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. |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |