Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple File Manipulation At Once

Status
Not open for further replies.

Dijinn

Programmer
Apr 4, 2003
2
TR

Greetings,
I recently wrote a macro that will swap out one word style through out a document with another word style of the user's choice. This works fine. The problem is that it is much too slow. We have thousands of files to nuke old styles from in several directories.

Would anyone know how to take a directory specified by the user and do the style manipulation in all the word docs in that directory? I've included my macro as is below:

Sub TempBackup()
'
' ChangeDocumentStyles Macro
' Macro created {4.2.2003} by {Taylor Holmes}
'
' Purpose: To provide a way to quickly find and replace specified
' styles and replace them with another specified style by the user.


Dim OriginalStyle As String
Dim ReplaceStyle As String
Dim msgPrompt1 As String
Dim msgPrompt2 As String
Dim msgTitle As String
Dim Continue As String


StartReplace:
msgPrompt1 = "Please enter the style name you'd like to replace."
msgPrompt2 = "Please enter the style name you'd like to replace it with."
msgPrompt3 = "Would you like to replace another style?"
msgTitle = "SRM: Style Replace Macro"

' Prompt the user for the style to find
OriginalStyle = InputBox(msgPrompt1, msgTitle)
If OriginalStyle = "" Then GoTo EndRoutine

ReplaceStyle = InputBox(msgPrompt2, msgTitle)
If ReplaceStyle = "" Then GoTo EndRoutine

' Use the active Word document.
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles(OriginalStyle)
.Replacement.Style = ActiveDocument.Styles(ReplaceStyle)
.Execute Replace:=wdReplaceAll
End With

Continue = MsgBox(msgPrompt3, vbYesNo, msgTitle)

If Continue = vbYes Then GoTo ChangeDocumentStyles


EndRoutine:

End Sub

Thanks for any help you might be able to offer in pointing me towards the right direction!
Dijinn
 
Well if you're looking for a more efficient algorithm... I don't know. But, if you want to know how to just run this code over every file in a folder, well that's not too hard. Use the FileSystemObject class (you'll need to reference it in), open up a folder using the related classes, and do a "for each file in folder.files" in it. Remember to check the file type before runnning the macro on it. You will also need to change the macro to be a function which takes in a word document. Use this inputed word document instead of active document.

Let me know if this is the type of thing you were looking for, or if you need me to elaborate more.

-Venkman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top