MS Word MACRO "Insert Text from File using multiple documents and different ranges

Anonymous
2021-12-07T14:52:07+00:00

I am trying to build a MACRO in MS WORD that will allow me to Insert "Text from File" using multiple documents and different ranges.

My goal (if possible) is to select the text I want to replace and then Run a Macro that will allow me to select the document and range from which to pull the information. In some situations, I will need to replace text using multiple files and ranges.

Current MACRO:

I have created the document files with bookmarks to ensure that I can pull the appropriate "Range" I just cannot figure out how to create a Macro that will allow me to select a file destination, the file itself or set the range.

Please help if possible.

Thanks

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. John Korchok 224.3K Reputation points Volunteer Moderator
    2021-12-07T17:35:20+00:00

    Your idea sounds like a reinvention of Word's AutoText feature. That allows you to store text in an attached template, then insert it wherever you need it. (Insert>Quick Parts>AutoText>Add Selection to AutoText).

    To select files in VBA, use a dialog box. Here is Microsoft's page on this: Application.FileDialog property (Word)

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2021-12-10T12:23:05+00:00

    John,

    Thanks for the response.

    I'm basically merging several smaller weekly reports into a compiled Monthly report. The "work completed" changes from week to week and therefore, the quick parts will not work for this scenario. I can do this manually using the "Text from File" option, but it is tedious since I have to do it for each week of the month to get the correct weekly comments.

    Any other suggestions?

    Jason

    0 comments No comments
  3. John Korchok 224.3K Reputation points Volunteer Moderator
    2021-12-10T16:18:57+00:00

    Word's Master Document feature might work for your scenario. You can assemble a set of sub-documents under a master for printing of creating a PDF. Master documents have a bad reputation for corruption, so they're only safe to use if you assemble it, print or PDF, then delete the master. Here's a macro that makes quick work of creating a master document from a folder full of files:

    Sub AssembleBook()
        Dim SubDocFile$, FolderPath$, Template$
        Dim Counter&
        Dim oFolder As FileDialog
        Dim oBookmark As Bookmark
        Dim oTOC As TableOfContents
    
    'Create a dynamic array variable, and then declare its initial size
        Dim DirectoryListArray() As String
        ReDim DirectoryListArray(1000)
    
    'Use the built-in Folder Picker dialog to choose the folder holding all the subdocuments
        Set oFolder = Application.FileDialog(msoFileDialogFolderPicker)
        With oFolder
            .AllowMultiSelect = False
            If .Show <> 0 Then
                FolderPath$ = .SelectedItems(1)
            Else
                GoTo EndSub 'If you cancel out of the dialog, this will exit the macro
            End If
        End With
        
    'Turn off display updating for a less distracting appearance
        Application.ScreenUpdating = False
        
    'Loop through all the files in the directory by using Dir$ function
        SubDocFile$ = Dir$(FolderPath$ & Application.PathSeparator & "*.*")
        Do While SubDocFile$ <> ""
            DirectoryListArray(Counter) = SubDocFile$
            SubDocFile$ = Dir$
            Counter& = Counter& + 1
        Loop
    
    'Reset the size of the array without losing its values by using Redim Preserve
        ReDim Preserve DirectoryListArray(Counter& - 1)
    
    'The following will sort the subdocuments by their name. You can prepend the name with a number to create any order you want them to be in the master document.
        WordBasic.SortArray DirectoryListArray()
        
    'Start the master document process
        ActiveWindow.ActivePane.View.Type = wdOutlineView
        ActiveWindow.View = wdMasterView
        Selection.EndKey Unit:=wdStory
        For x = 0 To (Counter& - 1)
            If IsNumeric(Left(DirectoryListArray(x), 1)) Then
                FullName$ = FolderPath$ & Application.PathSeparator & DirectoryListArray(x)
                Documents.Open FileName:=FullName$, ConfirmConversions:=False
                With Documents(FullName$)
                    .AttachedTemplate = Template$
                    For Each oBookmark In Documents(FullName$).Bookmarks
                        oBookmark.Delete
                    Next oBookmark
                    .Close SaveChanges:=True
                End With
                Selection.Range.Subdocuments.AddFromFile Name:=FullName$, ConfirmConversions:=False
            End If
        Next x
    
    'Optional TOC update for a master document that already has a table of contents
    '    For Each oTOC In ActiveDocument.TablesOfContents
    '        oTOC.Update
    '    Next oTOC
        
    'Switch back to Print Layout view and turn screen updating back on.
        ActiveWindow.ActivePane.View.Type = wdPrintView
        Application.ScreenUpdating = True
    EndSub:
    End Sub
    
    0 comments No comments
  4. Charles Kenyon 159.8K Reputation points Volunteer Moderator
    2021-12-10T20:20:58+00:00

    Graham Mayor may have created an Add-In that does this for you:

    Boiler - Insert a selection of documents by Graham Mayor, MVP

    2 people found this answer helpful.
    0 comments No comments