Roderick
09-24-2018, 02:20 AM
The following code finds a field in a document (highlighted), deletes it and replaces it with two new fields.
It seems to work as required, perhaps not as elegantly written as it could be, but it does the job.
Sub ReplaceFieldsV2()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
With Selection
.HomeKey Unit:=wdStory
.Find.ClearFormatting
With Selection.Find
.Text = "^d SEQ Table \* ARABIC"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Find.Execute
.Delete Unit:=wdCharacter, Count:=1
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"StyleRef " & """GA Numbered Heading 1""" & " \s", PreserveFormatting:=False
.TypeText Text:="-"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ " & """Table""" & " \* ARABIC \r 1", PreserveFormatting:=False
End With
ActiveWindow.View.ShowFieldCodes = False
Call SpecialUpdateFields
Application.ScreenUpdating = True
End Sub
This procedure just deals with the first field it comes to. What I want to do is for it to move on to the next field, replace it and then so on for all the other similar fields until no more are found.
I tried researching a For...Loop or a While...Wend but couldn't fathom out where and how to use these. And which would be the best one to employ in this case.
Could someone point me in the right direction, please?
Thanks
Roderick
It seems to work as required, perhaps not as elegantly written as it could be, but it does the job.
Sub ReplaceFieldsV2()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
With Selection
.HomeKey Unit:=wdStory
.Find.ClearFormatting
With Selection.Find
.Text = "^d SEQ Table \* ARABIC"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Find.Execute
.Delete Unit:=wdCharacter, Count:=1
.TypeText Text:=" "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"StyleRef " & """GA Numbered Heading 1""" & " \s", PreserveFormatting:=False
.TypeText Text:="-"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ " & """Table""" & " \* ARABIC \r 1", PreserveFormatting:=False
End With
ActiveWindow.View.ShowFieldCodes = False
Call SpecialUpdateFields
Application.ScreenUpdating = True
End Sub
This procedure just deals with the first field it comes to. What I want to do is for it to move on to the next field, replace it and then so on for all the other similar fields until no more are found.
I tried researching a For...Loop or a While...Wend but couldn't fathom out where and how to use these. And which would be the best one to employ in this case.
Could someone point me in the right direction, please?
Thanks
Roderick