#1
|
|||
|
|||
Insert space before and after target text
The “Replace” macro was based on Macropod’s code from “Conditional tied to trying to find a " " space character” (https://www.msofficeforums.com/word-...character.html), which I adopted for a slightly different purpose. That purpose is to search for text entered in an input box in Word and test the character immediately in front of and following the target text. If it is not a space, enter a space, otherwise, ignore.
The Replace macro inserts a space in the front of the target. I added a variation so that the macro also inserts a space after the target text. Both work well except: 1. If the target is at the beginning of a sentence following a blank row. 2. If the target text’s case contains any upper case letters. The case sensitivity is puzzling, especially when I’ve tried to shut it off on the code using MatchCase:=False and Option Compare Text. I want it to ignore case altogether. The other effect I want is for the macro to read certain characters that follow the target text and if found, desist from inserting a space. I tried this by setting the characters below to the CharName variable and adding the Instr statement to see if it does the job, but it doesn’t work. CharName = ",.?/\¬`!£$%^&*-+='@#~:;|<>¦-_{}[]()`¬'" If InStr(1, CharName, SearchText) = 0 Then .InsertAfter " " I’ve attached a short Word doc with text for testing (highlighted in yellow) and a text file containing the macro (for some reason, this site no longer allows a .docm type file to be attached). If anybody has any suggestions for making it work as described above, please let me know. Thank you. |
#2
|
||||
|
||||
It seems to me you're going about this the wrong way. Surely it would be better to ensure there is always a space (if appropriate) before and/or after whatever the problem content is at the time of insertion, rather than trying to clean it up afterwards.
Regarding punctuation issues (which are broader than just 'the beginning of a sentence following a blank row'), those can be handled by the use of an appropriate wildcard expression that tests what the preceding character is. As for case sensitivity, 'MatchCase' doesn't work with wildcards - wildcard expressions are always case-sensitive.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the reply, Paul. This came about because sometimes when I run a find and replace, I add a leading or trailing space to the search text to avoid instances where removing the text (replacing it with nothing) leaves NO space in the remaining gap. I thought a macro would be a good way to clean this up, since I would then have two words mashed together.
|
#4
|
||||
|
||||
Perhaps if you could give more details of what you're trying to Find/Replace and the F/R expressions you're using a better approach could be suggested.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Not sure I can define it better than I already did. I want to search for text, determine if it has a space on either side, and if it doesn't, to add a space unless the character after the last character of the target is one of the special characters shown above. And this should work wherever the target is in the document, including the beginning of a paragraph.
|
#6
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, StrFnd As String, CharExcl As String, StrTmp As String CharExcl = ",.(?)/\¬(!)£$%^&(*)(-)+=’'”@(#)~:;|<>¦_{}([)()" StrFnd = Trim(InputBox("What is the Text to Find")) If StrFnd = "" Then Exit Sub With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = StrFnd .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWildcards = True .Execute End With Do While .Find.Found i = i + 1 With .Duplicate StrTmp = .Text If Not .Characters.First.Previous.Text Like _ "[ ‘'“" & Chr(160) & Chr(12) & vbCr & vbTab & "]" Then StrTmp = " " & StrTmp End If If Not .Characters.Last.Next.Text Like _ "[ " & CharExcl & Chr(160) & Chr(12) & vbCr & vbTab & "]" Then If Not .Characters.Last.Next.Text = "]" Then StrTmp = StrTmp & " " End If End If End With If .Text <> StrTmp Then .Text = StrTmp .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True MsgBox i & " instances found." End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
That works, and works very well - thanks, Paul!
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
macro to find a character and insert space so autocorrect will expand | redzan | Word VBA | 3 | 05-22-2014 04:22 PM |
Insert space in front of a numberd list MS Word | ekesawi | Word | 1 | 12-05-2012 07:21 PM |
Envelope Printing is off target - need help please! | mylan | Word | 2 | 10-24-2012 12:24 PM |
Send OFT when target email arrives | RandWald | Outlook | 0 | 11-11-2011 10:52 AM |
Target line on a excel graph | leroytrolley | Excel | 1 | 01-16-2009 04:19 PM |