Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 09-07-2023, 08:05 AM
alejandro21805 alejandro21805 is offline Macro to find a keyword and replace it by inserting the content of another Word document Windows 10 Macro to find a keyword and replace it by inserting the content of another Word document Office 2019
Novice
Macro to find a keyword and replace it by inserting the content of another Word document
 
Join Date: Sep 2023
Posts: 3
alejandro21805 is on a distinguished road
Default

Hello gmaxey

Thank you very much for your help. I modified the macro to find keywords between ## and $$ symbols, and it works. I hope it will be useful for everyone.

Code:
Sub ReplaceKeyWord()
    Dim oRng As Range
    Dim strFolderPath As String
    Dim strKeyword As String
    Dim strFileName As String
    
    ' Define the folder where the files to be inserted are located
    strFolderPath = "C:\Users\File\" ' Replace with the correct path
    
    ' Define the keyword pattern between "##" and "$$"
    strKeyword = "##*$$" ' This pattern will search for any word that matches the pattern
    
    Set oRng = ActiveDocument.Content
    
    ' Loop to find and replace all instances of the keyword
    Do While oRng.Find.Execute(FindText:=strKeyword, MatchWildcards:=True)
        ' Get the filename to insert
        strFileName = strFolderPath & Mid(oRng.Text, 3, Len(oRng.Text) - 4) & ".docx"
        
        ' Check if the file exists
        If Dir(strFileName) <> "" Then
            ' Delete the found keyword
            oRng.Delete
            ' Insert the content of the file
            oRng.InsertFile FileName:=strFileName
        Else
            ' If the file doesn't exist, display an error message
            MsgBox "The file '" & strFileName & "' was not found in the specified folder.", vbExclamation, "File Not Found"
        End If
        
        ' Move the range to the end of the document to continue searching
        oRng.Collapse Direction:=wdCollapseEnd
    Loop
    
    ' Clean up and release the Range object
    Set oRng = Nothing
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to find a keyword and replace it by inserting the content of another Word document Find and Replace whole folder content - Word files Leslie Word VBA 7 03-01-2020 08:58 PM
Find and Replace Numbers Macro VBA in Microsoft Word Yotem189 Word VBA 3 09-20-2018 05:55 AM
Macro to find a keyword and replace it by inserting the content of another Word document Finding the graphics and inserting the keyword Ajay2506 Word VBA 4 06-28-2016 05:34 AM
Macro to find a keyword and replace it by inserting the content of another Word document Need Macro for Find and Replace, Inserting logo in Ms-Word. Aswinraj Word VBA 1 06-05-2016 04:33 PM
Macro to find a keyword and replace it by inserting the content of another Word document Word VBA Macro to Find and Replace based on the Alt Text of an Image bennymc Word VBA 1 01-27-2014 04:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:24 PM.


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