![]() |
|
#1
|
|||
|
|||
|
space before and after (+, ×, ÷, =, <, >) need to insert space while a character before only e.g (p+0.5, p×0.5) kindly help me on this there are lot spaces to be insert manually. |
|
#2
|
||||
|
||||
|
This is easier said than done as the 'x' is a symbol character and the '÷' sign cannot be used in a wildcard search string, so they have to be processed separately.
The following, however, works in your test document. See also Replace using wildcards Code:
Option Explicit
'Graham Mayor - https://www.gmayor.com - Last updated - 19 Mar 2022
'The Symbol function is based on code from https://wordmvp.com/FAQs/MacrosVBA/FindReplaceSymbols.htm.
Private OriginalRange As Range, oPara As Range, oChar As Range
Private vFindText As Variant
Private vReplaceText As Variant
Private oRng As Range
Private i As Long
Sub Macro1()
vFindText = Array("( [\>\<\+\=])([0-9])", "([! ])([\>\<\+\=])([0-9])")
vReplaceText = Array("\1 \2", "\1 \2 \3")
For i = 0 To UBound(vFindText)
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Execute findText:=vFindText(i), _
MatchWildcards:=True, _
Forward:=True, _
Wrap:=wdFindStop, _
Replacewith:=vReplaceText(i), _
Replace:=wdReplaceAll
End With
Next i
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=ChrW(247), _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindStop)
If oRng.Start = oRng.Paragraphs(1).Range.Start Then
oRng.Text = ChrW(247) & Chr(32)
Else
oRng.Text = Chr(32) & ChrW(247) & Chr(32)
End If
oRng.Collapse 0
Loop
End With
Call ReplaceSymbol(FindChar:=ChrW(-3916), _
FindFont:="Symbol", _
ReplaceChar:=-3916, _
ReplaceFont:="Symbol")
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
Sub ReplaceSymbol(FindChar As String, FindFont As String, _
ReplaceChar As String, ReplaceFont As String)
Set OriginalRange = Selection.Range
ActiveDocument.Range(0, 0).Select
With Selection.Find
.ClearFormatting
.Text = FindChar
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
'keep searching until nothing found
If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
'Insert the replacement symbol where the found symbol was
Set oChar = Selection.Range
Set oPara = oChar.Paragraphs(1).Range
If Not oChar.Start = oPara.Start Then
Selection.Text = Chr(32)
Selection.Collapse 0
End If
Selection.InsertSymbol Font:=ReplaceFont, _
CharacterNumber:=ReplaceChar, Unicode:=True
Selection.Collapse (0)
Selection.Text = Chr(32)
Selection.Collapse 0
Else
Selection.Collapse 0
End If
Loop
End With
OriginalRange.Select
lbl_Exit:
Set OriginalRange = Nothing
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 |
|
#3
|
|||
|
|||
|
while we replace highlight the replaced characters then we identify easily were they change.
Is that possible. |
|
#4
|
||||
|
||||
|
That's just a matter of applying a highlight to the replacements e.g
Code:
Option Explicit
'Graham Mayor - https://www.gmayor.com - Last updated - 19 Mar 2022
'The Symbol function is based on code from https://wordmvp.com/FAQs/MacrosVBA/FindReplaceSymbols.htm.
Private OriginalRange As Range, oPara As Range, oChar As Range
Private vFindText As Variant
Private vReplaceText As Variant
Private oRng As Range
Private i As Long
Private lHiLite As Long
Sub Macro1()
lHiLite = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdYellow
vFindText = Array("( [\>\<\+\=])([0-9])", "([! ])([\>\<\+\=])([0-9])")
vReplaceText = Array("\1 \2", "\1 \2 \3")
For i = 0 To UBound(vFindText)
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Execute findText:=vFindText(i), _
MatchWildcards:=True, _
Forward:=True, _
Wrap:=wdFindStop, _
Replacewith:=vReplaceText(i), _
Replace:=wdReplaceAll
End With
Next i
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=ChrW(247), _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindStop)
If oRng.Start = oRng.Paragraphs(1).Range.Start Then
oRng.Text = ChrW(247) & Chr(32)
Else
oRng.Text = Chr(32) & ChrW(247) & Chr(32)
End If
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With
Call ReplaceSymbol(FindChar:=ChrW(-3916), _
FindFont:="Symbol", _
ReplaceChar:=-3916, _
ReplaceFont:="Symbol")
lbl_Exit:
Options.DefaultHighlightColorIndex = lHiLite
Set oRng = Nothing
Exit Sub
End Sub
Sub ReplaceSymbol(FindChar As String, FindFont As String, _
ReplaceChar As String, ReplaceFont As String)
Set OriginalRange = Selection.Range
ActiveDocument.Range(0, 0).Select
With Selection.Find
.ClearFormatting
.Text = FindChar
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
'keep searching until nothing found
If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
'Insert the replacement symbol where the found symbol was
Set oChar = Selection.Range
oChar.HighlightColorIndex = wdYellow
Set oPara = oChar.Paragraphs(1).Range
If Not oChar.Start = oPara.Start Then
Selection.Text = Chr(32)
Selection.Range.HighlightColorIndex = wdYellow
Selection.Collapse 0
End If
Selection.InsertSymbol Font:=ReplaceFont, _
CharacterNumber:=ReplaceChar, Unicode:=True
Selection.Collapse (0)
Selection.Text = Chr(32)
Selection.Range.HighlightColorIndex = wdYellow
Selection.Collapse 0
Else
Selection.Collapse 0
End If
Loop
End With
OriginalRange.Select
lbl_Exit:
Set OriginalRange = Nothing
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 |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| insert nonbreaking space | balavaka | Word VBA | 1 | 02-10-2022 10:52 PM |
Using find and replace to insert an extra space after full stops
|
Johanna | Word | 33 | 03-07-2020 01:43 PM |
Can't insert space at the beginning of a line in Word
|
p89.schneider | Word | 6 | 03-24-2016 11:38 PM |
| Insert space before and after target text | Marrick13 | Word VBA | 6 | 06-20-2014 07:46 AM |
| Insert space in front of a numberd list MS Word | ekesawi | Word | 1 | 12-05-2012 07:21 PM |