Results 1 to 13 of 13
  1. #1
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hello,

    Thank you for taking the time to read this post.

    I am looking for a macro that can find and replace styles.
    I have about 30 styles, that I simply have to rename, across hundreds of documents.

    I would like to find a way (if possible) to:

    1. Point to a folder full of MS Word (.DOC) documents.
    2. Find old style name.
    3. Rename old style to new style name.
    4. Repeat until finished.

    Note: I only need to change the name. The formatting and everything else is the same as the original style name - just a simple name change.

    Is this possible to do? I know a lot may hinge on perhaps the Normal.dot file or any template that has been attached to the documents.

    Any information on how to go about doing this, is very much appreciated.

    Thanks in advance for the assistance.

    Regards,

    Jim

  2. #2
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hello Hans,

    Thanks for the macro. It is appreciated.

    I will test it out later tonight.

    Regards,

    Jim

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    You already know how to loop through documents.

    Sub ReplaceInDoc()
    Dim OldNames(1 To 30) As String
    Dim NewNames(1 To 30) As String
    Dim i As Integer
    OldNames(1) = "OldStyle"
    NewNames(1) = "NewStyle"
    OldNames(2) = "NextOldStyle"
    NewNames(2) = "NextNewStyle"
    ' etc. until 30
    On Error Resume Next
    For i = 1 To 30
    doc.Styles(OldNames(i)).NameLocal = NewNames(i)
    Next i
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    As in all the other procedures I wrote for you, the header should have been

    Sub ReplaceInDoc(doc As Document)

    Sorry about that.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Edited by HansV to reduce picture in size - please don't post pictures larger than 640 x 480 pixels.

    Hello Hans,

    I just got around to trying this macro now and have run into a compile error (see attached screen shot).

    The only thing I have modified (besides adding the looping function) was to change the number of styles from 30 to 27.

    One last thing. Is there a way I can put in a line of code that will delete all of the styles in each document - EXCEPT the styles that have been renamed?
    My goal is to rename these 27 styles globally and then to purge all other styles from the documents (regardless of the fact that a template (.dot) is attached to each chapter.

    The ending of the macro (off the page of the screen shot) looks like this:

    ' etc. until 27
    On Error Resume Next
    For i = 1 To 27
    doc.Styles(OldNames(i)).NameLocal = NewNames(i)
    Next i
    End Sub



    Thanks in advance for your assistance.

    Regards,

    Jim
    Attached Images Attached Images
    • File Type: jpg x.jpg (30.9 KB, 3 views)

  6. #6
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hello Hans,

    No problem.

    Listen, perhaps I am missing something in the code as the macro runs without any errors, but the document (one for now as a test) has not been touched, after looking at the time stamp.
    Also, I need a way to delete all styles in the document that are not the new styles listed in the string. Is there a simple command to put at the end, that will tell MS Word to purge all remaining styles?

    Thanks for your insight. Here is the macro, in its current form:



    Sub ReplaceInFolder()
    ' Modify as needed, but keep the trailing backslash
    Const strFolder = "C:Temp12"
    Dim strFile As String
    Dim doc As Document
    Application.ScreenUpdating = False
    strFile = Dir(strFolder & "*.doc")
    Do While Not strFile = ""
    Set doc = Documents.Open(strFolder & strFile)
    ReplaceInDoc doc
    doc.Close SaveChanges:=True
    strFile = Dir
    Loop
    Application.ScreenUpdating = True
    End Sub

    Sub ReplaceInDoc(doc As Document)
    Dim OldNames(1 To 27) As String
    Dim NewNames(1 To 27) As String
    Dim i As Integer
    OldNames(1) = "Body text"
    NewNames(1) = "Para Text"

    OldNames(2) = "Fig_Title"
    NewNames(2) = "Figure Title"

    OldNames(3) = "Heading 1,Chapter"
    NewNames(3) = "Heading 1"

    OldNames(4) = "Heading 2,Work_Package"
    NewNames(4) = "Heading 2"

    OldNames(5) = "Heading 3,Heading 3_style"
    NewNames(5) = "Heading 3"

    OldNames(6) = "initial setup title"
    NewNames(6) = "Heading 2 Ruled"

    OldNames(7) = "Normal,Normal_Para"
    NewNames(7) = "Para Text"

    OldNames(8) = "Para_Title_Ruled"
    NewNames(8) = "Heading 2 Ruled"

    OldNames(9) = "Paragraph text"
    NewNames(9) = "Para Text"

    OldNames(10) = "Paragraph_Title"
    NewNames(10) = "Heading 2"

    OldNames(11) = "Procedure bullet"
    NewNames(11) = "Bullet 1"

    OldNames(12) = "Procedure bullet 2"
    NewNames(12) = "Bullet 2"

    OldNames(13) = "Procedure Sub-Title"
    NewNames(13) = "Heading 3"

    OldNames(14) = "Procedure Title"
    NewNames(14) = "Heading 2"

    OldNames(15) = "Procedures"
    NewNames(15) = "Step Lvl 1"

    OldNames(16) = "Procedures_style"
    NewNames(16) = "Step Lvl 2"

    OldNames(17) = "Sub_Para_Title"
    NewNames(17) = "Heading 3"

    OldNames(18) = "table_step 1"
    NewNames(18) = "Table Step 1"

    OldNames(19) = "table_step a"
    NewNames(19) = "Table Step 2"

    OldNames(20) = "table_step_(1)"
    NewNames(20) = "Table Step 3"

    OldNames(21) = "table_step_(a)"
    NewNames(21) = "Table Step 4"

    OldNames(22) = "table_text"
    NewNames(22) = "Table Text"

    OldNames(23) = "table_text_centered"
    NewNames(23) = "Table Text Cen"

    OldNames(24) = "table_text_indented"
    NewNames(24) = "Table Text Indented"

    OldNames(25) = "Table_Title"
    NewNames(25) = "Table Title"

    OldNames(26) = "Tbl_Column_head"
    NewNames(26) = "Table Heading"

    OldNames(27) = "WCN_Text"
    NewNames(27) = "Warning"

    ' etc. until 27
    On Error Resume Next
    For i = 1 To 27
    doc.Styles(OldNames(i)).NameLocal = NewNames(i)
    Next i
    End Sub

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    I don't know why the documents wouldn't be changed - they do get saved in the loop.

    You cannot simply delete all styles - many styles are built-in and cannot be deleted. If you search this forum you'll find code that removes all styles that are not in use and not built in.

  8. #8
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hi Hans,

    I am not sure either, but I did notice that the new styles were added to the normal.dot file. This is not the template that I am using to attach
    to my documents. So, I will have to investigate it further.

    Does it make a difference if each document is protected (e.g. Tools>Protect Document and "Limit Formatting to a Selection of Styles") or not? In other words, should I unprotect each chapter before running this macro?

    Thanks,

    -J

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    You can easily test whether unprotecting the document before running ReplaceInDoc, then reprotecting it afterwards, makes a difference.

  10. #10
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hello Hans,

    Here is the update to this posting. Unprotecting the documents does not make a difference.

    The problem is that all these documents are linked to a template, from which contains all the approved styles.

    If I put the .dot (template) file in a temp folder on my "C" drive, change the macro code for .dot and then try to change just that file, I get the same results - which is nothing happens.

    Yes, the code will run without a problem, but both the .dot and regular .doc files are not touched.

    The only way I have seen this work to date, is if I create a blank word document that is based on normal.dot, make a few styles and rerun the macro. The macro works fine at that point.

    Do you have any idea on what can be done to get the macro to update these documents? Do I need to have the macro open or reference the .dot (template) file, like the my normal word documents?

    Thanks in advance for any suggestions offered.

    Regards,

    Jim

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    If you open one of the documents, and select Tools | Templates and Add-Ins, is the check box "Automatically update document styles" ticked?

  12. #12
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    Hello Hans,

    It's been a while since my last posting here.
    After some investigation and tweaking, I was able to get this macro to work. However the macro would not rename the following styles:
    Heading 1
    Heading 2
    Heading 3
    Normal

    These styles are tied to the Normal.dot file in MS Word.

    Is there a way to remap Heading 1, so that it changes to Heading 1a, Heading 2 to Heading 2a and Heading 3 to Heading 3a?

    As I mentioned before, the macro will rename all the styles, with the exception of the ones listed above. I would like to change that if possible.

    Thanks in advance for any suggestions offered.

    Regards,

    Jim

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to Find/Replace Word (MS Word 2003 (SP3))

    You cannot rename built-in styles such as Normal, Body Text or Heading 1. If you set the NameLocal property of Heading 1 to Heading 1a, the name will actually become Heading 1,Heading 1a. That is, the new name is tacked on to the built-in one. There is no way to actually change Heading 1 to Heading 1a. You could create a new style named Heading 1a, though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •