Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-06-2023, 07:42 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 Macro to find a keyword and replace it by inserting the content of another Word document

Hello everyone

I am working on a service portfolio, and I need to insert descriptions of the products I have in other Word documents that are saved in a folder. However, this portfolio changes depending on the type of client. To speed up the work, I have come up with the idea of placing a keyword in the main document (having the same name as the documents with the descriptions I need to insert). Through a macro, the goal is to replace the keyword and insert the content of the product descriptions (insert the Word documents) in its place. Here's a simple example:

01.Apple.Description


04.Orange.Description
10.Pineapple.Description

The keywords would be the names of the documents with the descriptions that I would need to insert. The idea is that the macro searches for the name within the folder and inserts the corresponding Word document into the main document.

Thank you in advance
Reply With Quote
  #2  
Old 09-06-2023, 10:58 AM
gmaxey gmaxey 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
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Something like this. Assumes your service portfolio document is in the same folder as your collection of description files and your description files are of the .docx format.


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim strPath As String
Dim strKeyWord As String
  Set oRng = ActiveDocument.Range
  strKeyWord = "Orange"
  With oRng.Find
    .Text = "Orange"
    If .Execute Then
      oRng.Text = vbNullString
      strPath = ThisDocument.path & "\"
      oRng.InsertFile strPath & strKeyWord & ".docx"
    End If
  End With
lbl_Exit:
  Exit Sub
  Resume
End Sub

That said, a better idea might just be to create building blocks in a global template addin and use the building blocks to define product descriptions.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-06-2023, 11:22 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 prompt response. The portfolio is a document that is not saved anywhere; in fact, it is a new document that each salesperson creates whenever a new request from a client arrives. The only documents I have saved are the description documents. Salespeople have access to the folder, but they do not have permissions to save new documents in it. That is why I would like the macro to work without the need to have the portfolio (main document) saved in a folder. Is it possible to do that with a macro?
Reply With Quote
  #4  
Old 09-06-2023, 02:29 PM
gmaxey gmaxey 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
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Yes, you could do that if you set the path in the code. So replace:
strPath = ThisDocument.path & ""
With
strPath = "Your known fixed folder path" 'e.g. "C:\Item Description Files"
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #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
  #6  
Old 09-08-2023, 04:28 AM
gmaxey gmaxey 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
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Glad to help.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



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
Macro to find a keyword and replace it by inserting the content of another Word document 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:00 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