Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 

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 09:02 AM.


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