Replace Formatting Styles in 3000 docs

P

PiaD

Hi -
I am new to Word macros....but I am so impressed with what they can do to
make a job easier. I need to find and replace old formatting styles with new
formatting styles in about 3000 documents. There are about 5 different
formatting styles used within each document that need to be replaced with 5
newer style formats (ie style: TextOLD to TextNew, and HeadingOld to
HeadingNew, etc).

Can you help me with a macro to find these old styles and replace with new
styles? Can this macro automatically perform this task on all files in a
directory? Or any other method that does not require me to open each and
every file to run the macro? Do I have to have the old style defined within
my template as well for it to find the style in the document?

Thank you so much for your help on this matter!
Best regards,
Pia
 
G

Greg Maxey

I don't know about 3000 documents at one time (might crash Word), but I
believe it could be done in batches with VBA.

It would now be a simple process to write your code from scratch. You might
download my VBA Find and Replace template AddIn to get and idea of the code
involve.

http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm

While it doesn't have the More>Format>Style feature on the User Interface,
you could easily add this functionality manually in the code itself. You
would want to edit the following function as shown:

Public Function SrchAndRplInStry(ByVal SrchRng As Range) As Boolean
Dim i As Integer
If bMultiWord Then
For i = 2 To UBound(ListArray)
With SrchRng.Find
.ClearFormatting
.Replacement.ClearFormatting
If Me.CheckBox1.Value = True Then .MatchCase = True
If Me.CheckBox2.Value = True Then .MatchWholeWord = True
If Me.CheckBox3.Value = True Then .MatchWildcards = True
.Text = ListArray(i, 1)
.Replacement.Text = ListArray(i, 2)
If Me.CheckBox6 Then
.Replacement.Highlight = True
End If
.Execute Replace:=wdReplaceAll
End With
Next i
Else
With SrchRng.Find
.ClearFormatting
.Replacement.ClearFormatting
If Me.CheckBox1.Value = True Then .MatchCase = True
If Me.CheckBox2.Value = True Then .MatchWholeWord = True
If Me.CheckBox3.Value = True Then .MatchWildcards = True
On Error GoTo Oops
.Format = True
*****************************************************************************
.Text = ""
*******************************************************************************
.Style = "OldStyleName"
*************************************************************************
.Replacement.Text = ""
*************************************************************************
.Replacement.Style = "NewStyleName"
If Me.CheckBox6 Then
.Replacement.Highlight = True
End If
.Execute Replace:=wdReplaceAll
End With
End If
SrchAndRplInStry = True
Exit Function
Oops:
If Err.Number = 5560 Then
MsgBox "The Find field contains an invalid pattern match expression.",
vbOKOnly, _
"Invalid Pattern."
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
SrchAndRplInStry = False
Err.Clear
ElseIf Err.Number = 5623 Then
MsgBox "The Replace field contains a group number which is out of range.",
vbOKOnly, _
"Invalid Group Number."
With Me.TextBox2
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
SrchAndRplInStry = False
Err.Clear
ElseIf Err.Number = 5625 Then
MsgBox "The characters ^& and ^c are invalid in the Find field.",
vbOKOnly, _
"Invalid Special Character."
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
SrchAndRplInStry = False
Err.Clear
ElseIf Err.Number = 5692 Then
MsgBox "The character you entered is not a valid special character in the"
_
& " in the Find field or is not supported when the Use wildcard" _
& " checkbox is selected.", vbOKOnly, "Invalid Character."
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
SrchAndRplInStry = False
Err.Clear
ElseIf Err.Number <> 0 Then
Dim Msg
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
SrchAndRplInStry = False
Err.Clear
End If
End Function

When you run the form you will have to enter something in the Find field to
prevent and error but it will be transparent.

Hi -
I am new to Word macros....but I am so impressed with what they can
do to make a job easier. I need to find and replace old formatting
styles with new formatting styles in about 3000 documents. There are
about 5 different formatting styles used within each document that
need to be replaced with 5 newer style formats (ie style: TextOLD to
TextNew, and HeadingOld to HeadingNew, etc).

Can you help me with a macro to find these old styles and replace
with new styles? Can this macro automatically perform this task on
all files in a directory? Or any other method that does not require
me to open each and every file to run the macro? Do I have to have
the old style defined within my template as well for it to find the
style in the document?

Thank you so much for your help on this matter!
Best regards,
Pia

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Greg Maxey

I don't know about 3000 documents at one time (might crash Word), but I
believe it could be done in batches with VBA.

It would now be a simple process to write your code from scratch.  You might
download my VBA Find and Replace template AddIn to get and idea of the code
involve.

http://gregmaxey.mvps.org/VBA_Find_And_Replace.htm

While it doesn't have the More>Format>Style feature on the User Interface,
you could easily add this functionality manually in the code itself.  You
would want to edit the following function as shown:

Public Function SrchAndRplInStry(ByVal SrchRng As Range) As Boolean
Dim i As Integer
If bMultiWord Then
  For i = 2 To UBound(ListArray)
    With SrchRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      If Me.CheckBox1.Value = True Then .MatchCase = True
      If Me.CheckBox2.Value = True Then .MatchWholeWord = True
      If Me.CheckBox3.Value = True Then .MatchWildcards = True
      .Text = ListArray(i, 1)
      .Replacement.Text = ListArray(i, 2)
      If Me.CheckBox6 Then
        .Replacement.Highlight = True
      End If
      .Execute Replace:=wdReplaceAll
    End With
  Next i
Else
  With SrchRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    If Me.CheckBox1.Value = True Then .MatchCase = True
    If Me.CheckBox2.Value = True Then .MatchWholeWord = True
    If Me.CheckBox3.Value = True Then .MatchWildcards = True
    On Error GoTo Oops
    .Format = True
***************************************************************************­**
    .Text = ""
***************************************************************************­****
    .Style = "OldStyleName"
*************************************************************************
    .Replacement.Text = ""
*************************************************************************
    .Replacement.Style = "NewStyleName"
    If Me.CheckBox6 Then
      .Replacement.Highlight = True
    End If
    .Execute Replace:=wdReplaceAll
  End With
End If
SrchAndRplInStry = True
Exit Function
Oops:
If Err.Number = 5560 Then
  MsgBox "The Find field contains an invalid pattern match expression.",
vbOKOnly, _
         "Invalid Pattern."
  With Me.TextBox1
   .SetFocus
   .SelStart = 0
   .SelLength = Len(.Text)
  End With
  SrchAndRplInStry = False
  Err.Clear
ElseIf Err.Number = 5623 Then
  MsgBox "The Replace field contains a group number which is out of range.",
vbOKOnly, _
         "Invalid Group Number."
  With Me.TextBox2
   .SetFocus
   .SelStart = 0
   .SelLength = Len(.Text)
  End With
  SrchAndRplInStry = False
  Err.Clear
ElseIf Err.Number = 5625 Then
  MsgBox "The characters ^& and ^c are invalid in the Find field.",
vbOKOnly, _
         "Invalid Special Character."
  With Me.TextBox1
   .SetFocus
   .SelStart = 0
   .SelLength = Len(.Text)
  End With
  SrchAndRplInStry = False
  Err.Clear
ElseIf Err.Number = 5692 Then
  MsgBox "The character you entered is not a valid special character inthe"
_
         & " in the Find field or is not supported when the Usewildcard" _
         & " checkbox is selected.", vbOKOnly, "Invalid Character."
  With Me.TextBox1
   .SetFocus
   .SelStart = 0
   .SelLength = Len(.Text)
  End With
  SrchAndRplInStry = False
  Err.Clear
ElseIf Err.Number <> 0 Then
Dim Msg
  Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & Err.Description
  MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
  SrchAndRplInStry = False
  Err.Clear
End If
End Function

When you run the form you will have to enter something in the Find field to
prevent and error but it will be transparent.








--
Greg Maxey -  Word MVP

My web sitehttp://gregmaxey.mvps.org
Word MVP web sitehttp://word.mvps.org

McCain/Palin '08 !!!- Hide quoted text -

- Show quoted text -

Let me correct myself.

It would "not" be a simple process to write your code from scratch.
 
P

PiaD

Hi Greg,
This is a very cool addin! I just ran through the batch process replacing
text and it worked great, replacing text within 20 files in just a few
seconds! Just one glitch - cannot kill the user interface with close/cancel
button. I get error 52 - bad file name? Any ideas to correct this?

Also - before I modify your function code as you kindly provided earlier, I
just want to verify one thing...where do I have to have the
newstyles/oldstyles defined for the function to work properly (i.e. what
template should the new styles be defined within or can/should I set the
template in the code?)

Thank you,
Pia
 
G

Greg Maxey

PiaD,

I am not having that problem here.

Can you open the VB Editor and put a Stop command in the first line of the
command button event for close/cancel then close the editor and try again.
When the editor opens at the Stop line, step through 1 line at a time using
the F8 key to identify what line is causing the error.

You got me with you second question. I am not such a wealth of information
when in comes to styles :-(. Basically the new styles have to be in the
template that the files are based on. If they are all based on the same
template then it shouldn't be a problem. If they are not, then I think you
will have to automate a process to attach a new template containing the
styles to each document.

I have another AddIn on my website for performing batch processes. You
might post another question on "How to attach a template containing updated
styles using VBA." Once you have that solved then you could run that as the
batch process.


Hi Greg,
This is a very cool addin! I just ran through the batch process
replacing text and it worked great, replacing text within 20 files in
just a few seconds! Just one glitch - cannot kill the user interface
with close/cancel button. I get error 52 - bad file name? Any ideas
to correct this?

Also - before I modify your function code as you kindly provided
earlier, I just want to verify one thing...where do I have to have the
newstyles/oldstyles defined for the function to work properly (i.e.
what template should the new styles be defined within or can/should I
set the template in the code?)

Thank you,
Pia

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Greg Maxey

Oh, the problem with close/cacel is probably your quicklist file location:

'You can change the path and file name of MyQuickList to suit your taste.
Private pFileName As String
Private bReviewed As Boolean
Private Const MyQuickList$ = "F:\MyQuickList.doc"

Create a MyQuickList.doc and save it anywhere. Then change the constant to
reflect the path.


Hi Greg,
This is a very cool addin! I just ran through the batch process
replacing text and it worked great, replacing text within 20 files in
just a few seconds! Just one glitch - cannot kill the user interface
with close/cancel button. I get error 52 - bad file name? Any ideas
to correct this?

Also - before I modify your function code as you kindly provided
earlier, I just want to verify one thing...where do I have to have the
newstyles/oldstyles defined for the function to work properly (i.e.
what template should the new styles be defined within or can/should I
set the template in the code?)

Thank you,
Pia

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Greg Maxey

I just played around with the recorder a bit and I think something like this
would do if you needed to copy the newstyles into each of the documents:

Application.OrganizerCopy Source:= _
"F:\My Documents\Word\Templates\AZ SCIP.dot", Destination:="Document1",
_
Name:="FE Table Heading", Object:=wdOrganizerObjectStyles

Where Source is the file name and path of the template containing the new
styles and destination is the current document being processed.

Name is the style name being copied.

Now with both the old and new styles as part of the document you should be
able to do the find and replace.

Hi Greg,
This is a very cool addin! I just ran through the batch process
replacing text and it worked great, replacing text within 20 files in
just a few seconds! Just one glitch - cannot kill the user interface
with close/cancel button. I get error 52 - bad file name? Any ideas
to correct this?

Also - before I modify your function code as you kindly provided
earlier, I just want to verify one thing...where do I have to have the
newstyles/oldstyles defined for the function to work properly (i.e.
what template should the new styles be defined within or can/should I
set the template in the code?)

Thank you,
Pia

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
P

PiaD

Yes - that was it. The macro works wonderfully. Now I will run some tests on
replacing style. Thank you!
 
P

PiaD

Would you be able to share with me how to modify your VBA Find and
Replace.dot function code to allow for mutiple style replacements vs. just
one style replacement?
 
P

PiaD

Hi Greg,
I would love to get the Batch Processing addin working for me. It would be
very useful. However, I get the following error message trying to execute a
find/replace Automate process (I do have both BatchProcess and
BatchProcessDocuments loaded): Error #91 was generated by Project Object
variable or With Block Variable not set. Any ideas?
 
G

Greg Maxey

PiaD,

Sorry, but I really don't have time to customize the AddIn further.
However, the answer you seek is already there. You would need to create an
array to hold the find style names and one to hold the replace with style
names and then loop through each. Just like what is currently being done for
multiwords.

Just hard code it in the same prodecure.

A basic example is:

Sub FRUsingAnArray1()
Dim SearchArray As Variant
Dim ReplaceArray as Variant
Dim myRange As Range
Dim i As Long
Dim TermString As String
SearchArray = Array("A", "B, "C")
ReplaceArray = Array("X", "Y", "Z")
ResetFRParameters
Set myRange = ActiveDocument.Range
For i = 0 To UBound(SearchArray)
With myRange.Find
.Text = SearchArray(i)
.Replacement.Text = ReplaceArray(i)
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

Would you be able to share with me how to modify your VBA Find and
Replace.dot function code to allow for mutiple style replacements vs.
just one style replacement?

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Greg Maxey

Hmm,

This one was much tougher to isoloate. Seems there was a timing issue in
the validateFindReplace function. I think I have it fixed so just download
the templates again from my website.

Note, I change from using a toolbar for the commands to a menu. I seem to
have better luck getting the commands to get picked up by Word2007 that way.

Hi Greg,
I would love to get the Batch Processing addin working for me. It
would be very useful. However, I get the following error message
trying to execute a find/replace Automate process (I do have both
BatchProcess and BatchProcessDocuments loaded): Error #91 was
generated by Project Object variable or With Block Variable not set.
Any ideas?

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
P

PiaD

Hi Greg,
How can I modify this so that if a name is not found in the array, it will
continue and not break for an error?

Thank You!
Pia
 
P

PiaD

This works so beautifully! I LOVE this Batch Processor! Thanks for
identifying the error so quickly!
 
G

Greg Maxey

PiaD,

You would normally use error handling for this sort of thing;

Sub FRUsingAnArray1()
Dim SearchArray As Variant
Dim ReplaceArray As Variant
Dim myRange As Range
Dim i As Long
Dim TermString As String
On Error GoTo 0
SearchArray = Array("Heading 1", "Heading 2", "Heading 3")
ReplaceArray = Array("Heading 4", "Heading 5", "Heading 3000")
Set myRange = ActiveDocument.Range
For i = 0 To UBound(SearchArray)
On Error GoTo Err_Handler
With myRange.Find
.Format = True
.Style = SearchArray(i)
.Replacement.Style = ReplaceArray(i)
On Error Resume Next
.Execute Replace:=wdReplaceAll
On Error GoTo 0
End With
Next
Err_ReEntry:
Exit Sub
Err_Handler:
MsgBox Err.Number 'You wouldn't keep this line
If Err.Number = 5834 Then
Resume Err_ReEntry
End If
End Sub

Hi Greg,
How can I modify this so that if a name is not found in the array, it
will continue and not break for an error?

Thank You!
Pia

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top