Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2014, 03:06 PM
Marrick13 Marrick13 is offline Insert space before and after target text Windows XP Insert space before and after target text Office 2010 32bit
Competent Performer
Insert space before and after target text
 
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
  #2  
Old 06-19-2014, 04:12 PM
macropod's Avatar
macropod macropod is offline Insert space before and after target text Windows 7 32bit Insert space before and after target text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-19-2014, 04:35 PM
Marrick13 Marrick13 is offline Insert space before and after target text Windows XP Insert space before and after target text Office 2010 32bit
Competent Performer
Insert space before and after target text
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

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.
Reply With Quote
  #4  
Old 06-19-2014, 04:37 PM
macropod's Avatar
macropod macropod is offline Insert space before and after target text Windows 7 32bit Insert space before and after target text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 06-19-2014, 06:46 PM
Marrick13 Marrick13 is offline Insert space before and after target text Windows XP Insert space before and after target text Office 2010 32bit
Competent Performer
Insert space before and after target text
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

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.
Reply With Quote
  #6  
Old 06-19-2014, 07:13 PM
macropod's Avatar
macropod macropod is offline Insert space before and after target text Windows 7 32bit Insert space before and after target text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 06-20-2014, 07:46 AM
Marrick13 Marrick13 is offline Insert space before and after target text Windows XP Insert space before and after target text Office 2010 32bit
Competent Performer
Insert space before and after target text
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

That works, and works very well - thanks, Paul!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert space before and after target text 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
Insert space before and after target text 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
Insert space before and after target text Target line on a excel graph leroytrolley Excel 1 01-16-2009 04:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:15 PM.


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