Sumit
Sumit

Reputation: 878

Writing a code to replace a character with another in VB in MS Word

I am new to VB. I need to write a VB script for my MS Word macro which would do the following:

  1. Search the entire document for the characters '-->'
  2. Replace this with a arrow symbol.

The following macro would simply add the arrow I want:

Sub Arrow()
'
' Arrow Macro
'
'
    Selection.InsertSymbol Font:="Times New Roman", CharacterNumber:=9658, _
        Unicode:=True
End Sub

The arrow symbol I want is like the following: Arrow

Can any one help me??

Upvotes: 0

Views: 2199

Answers (1)

genespos
genespos

Reputation: 3311

This is what you can get from Macro Recorder:

Sub Arrow()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "-->"
        .Replacement.Text = ChrW(9658)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Or you can use an old WordBasic command:

Sub Arrow()
    WordBasic.EditReplace Find:="-->", Replace:=ChrW(9658), Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=1
End Sub

Upvotes: 0

Related Questions