Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 03-16-2025, 08:06 AM
Jakster Jakster is offline Search for and replace merge fields in Word doc with plain text using VBA Windows 11 Search for and replace merge fields in Word doc with plain text using VBA Office 2021
Novice
Search for and replace merge fields in Word doc with plain text using VBA
 
Join Date: Mar 2025
Posts: 6
Jakster is on a distinguished road
Default

Just an update.... I think I may have found a way to make it work... I have used the following code (again, found online and modified) which seems to done the trick.

Code:
Sub ReplaceFields(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)

    Dim oFld As Field
    Dim oRng As Range
    
    For Each oFld In ActiveDocument.Fields
        Set oRng = oFld.Code
        If InStr(1, oRng.Text, strSearch) > 0 Then
        oRng.MoveStart wdCharacter, -1
        oRng.Collapse
        oRng.InsertAfter strReplace
        oFld.Delete
        End If
    Next oFld
    
lbl_Exit:
    Set oFld = Nothing
    Set oRng = Nothing
    Exit Sub
    
End Sub

Last edited by Jakster; 03-16-2025 at 06:02 PM.
Reply With Quote
 

Tags
mailmerge, ms-word, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for and replace merge fields in Word doc with plain text using VBA Plain text help with returns - looking for marker to use when converting to plain text redzan Word 1 04-18-2017 06:17 PM
Macro to keep formatted form fields after mail merge or replace text with formatted form fields jer85 Word VBA 2 04-05-2015 10:00 PM
Searhc for Hyperlinked Word and replace with plain text or nothing somniloquist Word 3 10-04-2011 02:33 AM
Search for and replace merge fields in Word doc with plain text using VBA Replace all occurrences of one merge field with plain text blankenthorst Mail Merge 5 07-01-2011 05:18 AM
Replace All with plain text containing subscript DeaducK Word 0 06-24-2010 08:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:12 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