View Single Post
 
Old 06-19-2014, 03:06 PM
Marrick13 Marrick13 is offline Windows XP Office 2010 32bit
Competent Performer
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default 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.
Attached Files
File Type: txt InsertSpaceBeforeandAfterText Code.txt (4.9 KB, 13 views)
File Type: docx TEST of Insert Space Macro (2).docx (22.4 KB, 14 views)
Reply With Quote