Apr 26 2018

Word VBA Macro – Find a word and replace with field code

I have this word document with the following content:

Makan

Nasi minum

Minum

Minum

Makan

Nasi minum

Minum

Minum

Makan

Nasi minum

Minum

Minum

 

I want Makan to be replace with a series of running number. Thus, need replace Makan with a field code of {SEQ ident}

The following VBA works, at least for me

Sub FindWords()
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = “Makan”
.Forward = True
End With
Do While Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
“SEQ ident”, PreserveFormatting:=True
Loop
End Sub

As a result,

1

Nasi minum

Minum

Minum

2

Nasi minum

Minum

Minum

3

Nasi minum

Minum

Minum

 

With “Toggle Field Codes”,

{ SEQ ident \* MERGEFORMAT }
Nasi minum
Minum
Minum
{ SEQ ident \* MERGEFORMAT }
Nasi minum
Minum
Minum
{ SEQ ident \* MERGEFORMAT }
Nasi minum
Minum
Minum