<G, D & R V,V,F>
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
message 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
Greg Maxey wrote:
Graham,
Well said. Perfectly expressed. I missed the part on omiting the
dashes.
Graham Mayor wrote:
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
Greg Maxey wrote:
Use wildcards. Find:
\(([0-9]{3})\)(-[0-9]{3}-[0-9]{4})
Replace with:
1-\1\2
vvskpk wrote:
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