Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find and Replace any digit

19 views
Skip to first unread message

vvskpk

unread,
Nov 27, 2009, 12:15:01 AM11/27/09
to
I am working in Word 2007.

I am trying for a different find and replace option but I am not getting
what I want. Here is the scenario:

I have many documents in which the phone numbers are given as
(000)-000-0000. I want all the numbers to replace with +1 prefixed and
without brackets and dashes, like this, +1 000 000 0000.
Now, I am able to find the numbers using ^# (any digit) in "Find What" by
entering(^#^#^#)-^#^#^#-^#^#^#^#.
But couldn't replace with the format I am looking for which is +1 ^#^#^#
^#^#^# ^#^#^#^# (+1 prefixed and no dashes).
It is giving me error when I give in Replace With field.

I can still use Find What using the any digit option, but manually need to
retype, hence, it will be really great if someone can give me the easiest way
to use Replace command. There are many documents with minimum 100 pages and
with phone numbers entered in different locations which I need to replace
with the format I required. Again, I am using Vista and Word 2007

Many thanks in advance!


vvskpk

Greg Maxey

unread,
Nov 27, 2009, 12:26:02 AM11/27/09
to
Use wildcards. Find:

\(([0-9]{3})\)(-[0-9]{3}-[0-9]{4})

Replace with:

1-\1\2

Graham Mayor

unread,
Nov 27, 2009, 12:59:14 AM11/27/09
to
It should be
\(([0-9]{3})\)-([0-9]{3})-([0-9]{4})
replace with
+1 \1 \2 \3
if you want to remove the dashes also.
http://www.gmayor.com/replace_using_wildcards.htm

You can paste these strings to the dialog boxes in the following batch
process to search all the documents in a folder
http://www.gmayor.com/installing_macro.htm

Public Sub BatchReplaceAnywhere()
'Macro by Doug Robbins - 1st March 2004
'with additional input from Peter Hewett
'and Graham Mayor 'to replace text in all the documents in a folder
Dim FirstLoop As Boolean
Dim myFile As String
Dim strPath As String
Dim myDoc As Document
Dim rngstory As Word.Range
Dim findText As String
Dim Replacement As String
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.title = "Select Folder containing the documents to be modifed and click
OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
'Close any documents that may be open
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(strPath & "*.doc")
While myFile <> ""
'Get the text to be replaced and the replacement
If FirstLoop = True Then
findText = InputBox("Enter the text that you want to replace.", _
"Batch Replace Anywhere")
If findText = "" Then
MsgBox "Cancelled by User"
Exit Sub
End If
TryAgain:
Replacement = InputBox("Enter the replacement text.", _
"Batch ReplaceAnywhere ")
If Replacement = "" Then
Response = MsgBox("Do you just want to delete the found text?",
_
vbYesNoCancel)
If Response = vbNo Then
GoTo TryAgain
ElseIf Response = vbCancel Then
MsgBox "Cancelled by User."
Exit Sub
End If
End If
FirstLoop = False
End If
'Open each file and make the replacement
Set myDoc = Documents.Open(strPath & myFile)
' Fix the skipped blank Header/Footer problem
MakeHFValid
' Iterate through all story types in the current document
For Each rngstory In ActiveDocument.StoryRanges
' Iterate through all linked stories
Do
SearchAndReplaceInStory rngstory, _
findText, Replacement
' Get next linked story (if any)
Set rngstory = rngstory.NextStoryRange
Loop Until rngstory Is Nothing
Next
'Close the file, saving the changes.
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub

Public Sub SearchAndReplaceInStory(ByVal rngstory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
'This routine supplied by Peter Hewett
Do Until (rngstory Is Nothing)
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set rngstory = rngstory.NextStoryRange
Loop
End Sub

Public Sub MakeHFValid()
Dim lngJunk As Long
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org

Greg Maxey

unread,
Nov 27, 2009, 1:24:50 AM11/27/09
to
Graham,

Well said. Perfectly expressed. I missed the part on omiting the dashes.

Graham Mayor

unread,
Nov 27, 2009, 1:46:39 AM11/27/09
to
I missed it too until I tested the macro and saw the result ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Greg Maxey

unread,
Nov 27, 2009, 8:10:25 AM11/27/09
to
Considering your thoroughness, I think you should be absolved of the crime
of expressing yourself poorly. You should not have to care that heavy
burden of guilt.
Do you think it would do any good to petition an appeal from the self
appointed newsgroup courtesy and expression quality policeman?

Graham Mayor wrote:
> I missed it too until I tested the macro and saw the result ;)
>
>

Doug Robbins - Word MVP

unread,
Nov 27, 2009, 8:43:34 AM11/27/09
to

I guess you meant to say "carry", not "care"

But also, did you get the gender of the member of the police service
correct?

<G, D & R V,V,F>

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Greg Maxey" <gma...@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message
news:#X$j6L2bK...@TK2MSFTNGP05.phx.gbl...

Greg Maxey

unread,
Nov 27, 2009, 9:05:47 AM11/27/09
to
> I guess you meant to say "carry", not "care"

You may often wonder, but never know. It could have been intentional and
fodder for my critics. ;-)

Doug Robbins - Word MVP wrote:
> I guess you meant to say "carry", not "care"
>

>

vvskpk

unread,
Nov 29, 2009, 8:47:01 PM11/29/09
to
Hi
Thanks for your kind suggestions. BUT I am sorry to say that it's not
working. I tried putting this in the Find and Replace but is not working. In
Find What I typed/copied "\(([0-9]{3})\)-([0-9]{3})-([0-9]{4})" and in
Replace with I typed "+1 \1 \2 \3". It is giving me error saying "Word has
finished searching the document. The search item was not found". If this
thread is still active, please suggest new or correct exisiting one. I am
posting this again hoping that this thread is not active.

Please help me! Really appreacite your authenticity, knowledge, experience
and transparency in sharing about the subject.

"Greg Maxey" wrote:

> .
>

vvskpk

unread,
Nov 29, 2009, 9:03:01 PM11/29/09
to
Re: Find and Replace any digit

I am posting this again hoping that earlier thread is not active/went
behind. You can reply if the earlier thread is active. Very Sorry for
occupying the space.

Everyone!


Thanks for your kind suggestions. BUT I am sorry to say that it's not
working. I tried putting this in the Find and Replace but is not working. In
Find What I typed/copied "\(([0-9]{3})\)-([0-9]{3})-([0-9]{4})" and in
Replace with I typed "+1 \1 \2 \3". It is giving me error saying "Word has
finished searching the document. The search item was not found".

New viewers!
I need a tag to findout the format (000)-000-0000 and replace with +1 000
000 0000 (+1 prefix and no dashes). This is to replace all phone number
format in a big number of document(s). I am using Vista and Office 2007.
Please check the thread "Find and Replace any digit" and the vaulable
suggestions given by experts, but may be something is missing in that.

Please understand and help me! Really appreacite your authenticity,

knowledge, experience and transparency in sharing about the subject.

_________________

Greg Maxey

unread,
Nov 29, 2009, 9:20:34 PM11/29/09
to
Have you checked the options "More" "Use Wildcards?"

Greg Maxey

unread,
Nov 29, 2009, 9:20:49 PM11/29/09
to
Have you checked the options "More" "Use Wildcards?"

Pamelia Caswell via OfficeKB.com

unread,
Nov 29, 2009, 9:27:22 PM11/29/09
to

This is a special kind of search. You must put a check in the card box.
(Click "more" in the find dialog to reveal more options.)

HTH,
Pam

vvskpk wrote:
>Hi
>Thanks for your kind suggestions. BUT I am sorry to say that it's not
>working. I tried putting this in the Find and Replace but is not working. In
>Find What I typed/copied "\(([0-9]{3})\)-([0-9]{3})-([0-9]{4})" and in
>Replace with I typed "+1 \1 \2 \3". It is giving me error saying "Word has
>finished searching the document. The search item was not found". If this
>thread is still active, please suggest new or correct exisiting one. I am
>posting this again hoping that this thread is not active.
>
>Please help me! Really appreacite your authenticity, knowledge, experience
>and transparency in sharing about the subject.
>

>> > I guess you meant to say "carry", not "care"
>>

>[quoted text clipped - 169 lines]
>>
>> .

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-docmanagement/200911/1

0 new messages