Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 22,513
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
 

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 06:14 PM.


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