#1
|
|||
|
|||
Find & Replace text in Field Code across multiple documents
Does anyone know if its possible to create a macro to find and replace a portion of field code across multiple documents?
The fastest way I can do it is:
FYI: the field code being replaced is a link to an excel spreadsheet. I wanted to use relative links, but apparently that's not possible We duplicate a large bunch of documents for every project at work. Some generic info has to be replaced in each document as per the project & this is the fastest way I can think of doing it. |
#2
|
||||
|
||||
I take it that we can assume this is a hyperlink field, in which case the following macro will work (replace the paths etc as appropriate) when used as a custom process with
http://www.gmayor.com/document_batch_processes.htm which will perform the file/folder handling. Code:
Function ReplaceLink(oDoc As Document) On Error GoTo err_Handler Dim oRng As Range Dim hLink As Hyperlink Set oDoc = ActiveDocument For Each hLink In oDoc.Hyperlinks If hLink.Address = "C:\Path\TargetWorkbook.xlsx" Then With hLink .Address = "C:\Path\Replacement.xlsx" .TextToDisplay = "C:\Path\Replacement.xlsx" .ScreenTip = "Click to open workbook" .Target = "C:\Path\Replacement.xlsx" End With End If Next hLink ReplaceLink = True lbl_Exit: Exit Function err_Handler: ReplaceLink = False Err.Clear Resume lbl_Exit End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Quote:
https://www.msofficeforums.com/word/...nal-files.html On the same page you'll find an attachment containing a macro that can be used to achieve an effect comparable to using relative paths for LINK fields as well. If that's a once-off exercise for each document, that's probably not the optimum approach. A better approach might be to use a macro in the document template that automatically populates any documents based on that template with the data from the relevant Excel file the first time the file is saved to the appropriate folder (which the macro might enforce as soon as the document is created). After all, if the Excel data should remain static once the document is populated, you probably no longer need the links.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Thanks for the replies guys.
@Macropod, I actually used your post on windowssecrets as a reference. It has been very helpful; however, I don't have any VBA experience, so I'm somewhat stumbling along with this one. As you say, the relative links work fine for INCLUDEPICTURE, which I'm using for updating our clients logo on each document. Its just the excel links that are causing an issue for me. @gmayor, I've been playing around with your add-in. How do I integrate the macro you've referred to? Essentially, I'd like to be able to batch process all the documents to replace the project reference with a new one. Code:
LINK Excel.Sheet.12 "\\\\C:\Users\Public\Documents\\Project 1\\Production\\Reference material\\Project Info.xlsx" Data!R2C3 \a \t \* MERGEFORMAT } LINK Excel.Sheet.12 "\\\\C:\Users\Public\Documents\\Project 2\\Production\\Reference material\\Project Info.xlsx" Data!R2C3 \a \t \* MERGEFORMAT } |
#5
|
||||
|
||||
As indicated in his post, Graham's macro assumed the use of hyperlinks, not LINK fields.
When you create a new project, for which the path appears to be based on C:\Users\Public\Documents\Project 1\Production\Reference material\ with 'Project 1' being variable: 1. In which folder are the new Word files located? 2. Are all the Word files based on the same template? 3. Are the links meant to disappear once they've been updated to point to the correct folder and the current data have been captured (i.e. should their content ever change)?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks macropod
1. The word documents are located in the folder "Production", whilst the spreadsheet is in "Reference material". 2. Yes, the word files are based on the same template. 3. It would be ideal if the links could remain, in case some information in the spreadsheet were to change. |
#7
|
||||
|
||||
Paul is correct. I had assumed hyperlink fields, but the general premise will work for Link fields e.g.
Code:
Function ReplaceLink(oDoc As Document) On Error GoTo err_Handler Dim oRng As Range Dim oFld As Field ActiveWindow.View.ShowFieldCodes = True For Each oFld In oDoc.Fields If oFld.Type = wdFieldLink Then If InStr(1, oFld.code, "Project 1") > 0 Then Set oRng = oFld.code oRng.Text = Replace(oRng.Text, "Project 1", "Project 2") oFld.code = oRng oFld.Update End If End If Next oFld ActiveWindow.View.ShowFieldCodes = False ReplaceLink = True lbl_Exit: Exit Function err_Handler: ReplaceLink = False Err.Clear Resume lbl_Exit End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
field code, vba find and replace |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text and replace with form field containing that text | iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM |
Macro to find coloured text and replace with form-field/formtext containing that text | tarktran | Word VBA | 1 | 11-26-2014 08:12 AM |
Macro for find/replace (including headers and footers) for multiple documents | jpb103 | Word VBA | 2 | 05-16-2014 04:59 AM |
How do I find/replace the same word in multiple documents? | Ineedhelp! | Word | 3 | 03-04-2014 03:50 PM |
Find and replace multiple documents change style | BaPW | Word | 0 | 08-14-2011 11:12 AM |