![]() |
#1
|
|||
|
|||
![]()
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. |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
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. |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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. |
#6
|
||||
|
||||
![]()
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 |
#7
|
|||
|
|||
![]()
Works fine gmayor.
Thanks all
__________________
Backup your original file before doing any modification. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
RPM7 | Word VBA | 6 | 05-12-2017 12:58 AM |
![]() |
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 |
![]() |
errtu | Word | 1 | 01-31-2013 02:09 PM |