Writing Macro for word in VBA..

Amit Saste
@amit-saste-58tp36 Oct 26, 2024

Hi All,

Please help me for writing macro in VBA for removing leading as well as trailing spaces of relational as well as arithmetic operators.
I tried using "Trim" as well as "$Trim" functuion from VB...but it does not remove all the leading and trailing spaces.Please see copy and paste of the code.Guide me!!
Sub Operator_Spaces()
Set myRange = ActiveDocument.Content
Dim Operators(1 To 21) As String
Dim i, j, k, Table_Cnt As Long
Dim Find_Match As String
Operators(1) = " < "
Operators(2) = " > "
Operators(3) = " <= "
Operators(4) = " >= "
Operators(5) = " <"
Operators(6) = " >"
Operators(7) = " <="
Operators(8) = " >="
Operators(9) = "< "
Operators(10) = "> "
Operators(11) = "<= "
Operators(12) = ">= "
Operators(13) = " + "
Operators(14) = " - "
Operators(15) = " / "
Operators(16) = " +"
Operators(17) = " -"
Operators(18) = " /"
Operators(19) = "+ "
Operators(20) = "- "
Operators(21) = "/ "

myRange.WholeStory
For i = 1 To 21
With myRange.Find
.Text = Operators(i)
Find_Match = Operators(i)
.Replacement.Text = Trim(Find_Match)
.Replacement.Font.Color = wdColorAutomatic
.Replacement.Font.Subscript = False
.Execute Replace:=wdReplaceAll
End With

For Each Table In ActiveDocument.Tables
With Table
With myRange.Find
.Text = Operators(i)
Find_Match = Operators(i)
.Replacement.Text = Strings.$Trim(Find_Match)
.Replacement.Font.Color = wdColorAutomatic
.Replacement.Font.Subscript = False
.Execute Replace:=wdReplaceAll
End With
End With
Next Table
Next i

Aso please suggest me for removing the spaces before or after that of special symbols..like £,³ & ¹.

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform