Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-22-2018, 06:45 PM
eduzs eduzs is offline Find and replace a text with a field Windows 10 Find and replace a text with a field Office 2010 32bit
Expert
Find and replace a text with a field
 
Join Date: May 2017
Posts: 266
eduzs is on a distinguished road
Default Find and replace a text with a field

I need a code to find and replace a text "Name" with a field "{ DOCVARIABLE MyVar \* MERGEFORMAT }".
Thanks all

__________________
Backup your original file before doing any modification.
Reply With Quote
  #2  
Old 02-22-2018, 08:14 PM
macropod's Avatar
macropod macropod is offline Find and replace a text with a field Windows 7 64bit Find and replace a text with a field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,366
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

It's a waste of time trying to replace the displayed content of a field as it will revert to its previous value immediately anything causes the field to refresh; you need to change the contents of the variable itself. That can be done with code like:
ActiveDocument.Variables("MyVar").Value = "some name"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-23-2018, 02:52 AM
eduzs eduzs is offline Find and replace a text with a field Windows 10 Find and replace a text with a field Office 2010 32bit
Expert
Find and replace a text with a field
 
Join Date: May 2017
Posts: 266
eduzs is on a distinguished road
Default

Thanks. I know that.
In my project I have some docvariables in many documents, it's all works fine.
Those fields will be filled automatically with actvdoc.variables("xxxx").value = "zzzz" / fiels update / fields unlink, to create a document.
The problem is that I want to change a single word with another field (docvariable).
I need to automatically find/replace a text with this field.
Thanks
__________________
Backup your original file before doing any modification.
Reply With Quote
  #4  
Old 02-23-2018, 04:02 AM
macropod's Avatar
macropod macropod is offline Find and replace a text with a field Windows 7 64bit Find and replace a text with a field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,366
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Pperhaps you could be clearer about what you're trying to do. For example, what are you trying to change - the variable named in the DOCVARIABLE field, or the displayed value? If it's the latter, see my previous reply.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-23-2018, 04:28 AM
eduzs eduzs is offline Find and replace a text with a field Windows 10 Find and replace a text with a field Office 2010 32bit
Expert
Find and replace a text with a field
 
Join Date: May 2017
Posts: 266
eduzs is on a distinguished road
Default

Thanks. It's simple to explain, suppose that I have a simple plain text in a DOCX file like that:
"That beautiful blue house at the hill belongs to Mary a good person."
I want to do a find/replace the text with a field (all occurrences), that the paragraph will be:
"That beautiful blue house at the hill belongs to { DOCVARIABLE TheName \* MERGEFORMAT } a good person."
I dont think useful to explain why doing this, because it is complex and would take a lot of time.
Thanks.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #6  
Old 02-23-2018, 04:41 AM
gmayor's Avatar
gmayor gmayor is offline Find and replace a text with a field Windows 10 Find and replace a text with a field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following should work

Code:
Sub Macro1()
ReplaceWithDocVar "Mary", "TheName"
End Sub


Sub ReplaceWithDocVar(strFind As String, strVarName As String)
Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        With oStory.Find
            Do While .Execute(FindText:=strFind, MatchWholeWord:=True)
                oStory.Text = ""
                oStory.Fields.Add Range:=oStory, _
                                  Type:=wdFieldDocVariable, _
                                  Text:=Chr(34) & strVarName & Chr(34), _
                                  PreserveFormatting:=True
                oStory.Collapse 0
            Loop
        End With
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                With oStory.Find
                    Do While .Execute(FindText:=strFind, MatchWholeWord:=True)
                        oStory.Text = ""
                        oStory.Fields.Add Range:=oStory, _
                                          Type:=wdFieldDocVariable, _
                                          Text:=Chr(34) & strVarName & Chr(34), _
                                          PreserveFormatting:=True
                        oStory.Collapse 0
                    Loop
                End With
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 02-23-2018, 05:19 PM
eduzs eduzs is offline Find and replace a text with a field Windows 10 Find and replace a text with a field Office 2010 32bit
Expert
Find and replace a text with a field
 
Join Date: May 2017
Posts: 266
eduzs is on a distinguished road
Default

Works fine gmayor.
Thanks all
__________________
Backup your original file before doing any modification.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace a text with a field Find & Replace text in Field Code across multiple documents RPM7 Word VBA 6 05-12-2017 12:58 AM
Find and replace a text with a field 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
search and replace a field containing text and numbers H28Sailor Word 6 09-11-2014 01:00 AM
Find and replace a text with a field Find and replace a string of text errtu Word 1 01-31-2013 02:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:10 AM.


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