![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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?
|
|
#4
|
|||
|
|||
|
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" |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
Glad to help.
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Finding the graphics and inserting the keyword
|
Ajay2506 | Word VBA | 4 | 06-28-2016 05:34 AM |
Need Macro for Find and Replace, Inserting logo in Ms-Word.
|
Aswinraj | Word VBA | 1 | 06-05-2016 04:33 PM |
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 |