Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-18-2022, 07:31 PM
stky stky is offline can we insert space Windows 10 can we insert space Office 2013
Advanced Beginner
can we insert space
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default can we insert space

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.
Attached Files
File Type: docx Need to change if before and after space.docx (11.6 KB, 9 views)
Reply With Quote
  #2  
Old 03-19-2022, 02:32 AM
gmayor's Avatar
gmayor gmayor is offline can we insert space Windows 10 can we insert space Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 03-19-2022, 02:44 AM
stky stky is offline can we insert space Windows 10 can we insert space Office 2013
Advanced Beginner
can we insert space
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default

while we replace highlight the replaced characters then we identify easily were they change.

Is that possible.
Reply With Quote
  #4  
Old 03-19-2022, 03:35 AM
gmayor's Avatar
gmayor gmayor is offline can we insert space Windows 10 can we insert space Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert nonbreaking space balavaka Word VBA 1 02-10-2022 10:52 PM
can we insert space Using find and replace to insert an extra space after full stops Johanna Word 33 03-07-2020 01:43 PM
can we insert space 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft