Replacing text in MS Word with absolutely nothing

User avatar
geecee
3StarLounger
Posts: 357
Joined: 02 Jun 2013, 05:55
Location: Australia - SOUTH MORANG - A northern suburb of the city of MELBOURNE in the state of Victoria

Replacing text in MS Word with absolutely nothing

Post by geecee »

I have a Word document with about 100 names, each of which is separated by a comma and a blank space. What I wish to do is delete certain names, the comma and the blank space from the list and replace them with absolutely nothing. The following code does this but just one at a time, replacing the sample with the actual text. What I would like is to have the option to select up to a certain unknown number i.e until all I want to delete is selected. Hope thgis makes sense. Any help would be much appreciated. Thanks.

Code: Select all

Option Explicit
Sub replaceSampleToNothing()
    Dim sample
    sample = "sampleText"

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = sample
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
George

When we're gaun up the hill o’ fortune, may we ne'er meet a frien' comin' doun!
(When we are going up the hill of fortune, may we never meet a friend coming down!)

:smile: Don't cry because it's over...Smile because it happened.l :smile:

:note: At the end of the day it's midnight. :note:


User avatar
HansV
Administrator
Posts: 78930
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replacing text in MS Word with absolutely nothing

Post by HansV »

Do you want to replace several names with nothing? If so, you could use code like this (modify to suit your needs):

Code: Select all

Sub replaceSampleToNothing()
    Dim sample
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        For Each sample In Array("thisname, ", "thatname, ", "othername, ")
            .Execute FindText:=sample, ReplaceWith:="", Replace:=wdReplaceAll
        Next sample
    End With
End Sub
Best wishes,
Hans

User avatar
geecee
3StarLounger
Posts: 357
Joined: 02 Jun 2013, 05:55
Location: Australia - SOUTH MORANG - A northern suburb of the city of MELBOURNE in the state of Victoria

Re: Replacing text in MS Word with absolutely nothing

Post by geecee »

Thanks Hans. Perfect.
George

When we're gaun up the hill o’ fortune, may we ne'er meet a frien' comin' doun!
(When we are going up the hill of fortune, may we never meet a friend coming down!)

:smile: Don't cry because it's over...Smile because it happened.l :smile:

:note: At the end of the day it's midnight. :note: