Reply
 
Thread Tools Display Modes
  #1   Report Post  
catmanbaloo
 
Posts: n/a
Default How can I automatically rename a whole batch of word files?

I recently had to recover a great number of files, but unfortunately although
all the information is in the word documents, the titles are all file01,
file02 etc. I was wondering if there exists a tool that would allow me to
rename the files based on the first few words of the document? This would
save us many hours of boring detailed work.
  #2   Report Post  
Greg Maxey
 
Posts: n/a
Default

With some code tips from Jay Freedman, Doug Robbins and JGM, I was able to
put together the following. Put all of your files in a single folder (I
used "C:\Batch Folder" and run the macro:

Public Sub BatchReNameFiles()

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim NewName As String
Dim OldName As String
Dim oRng As Range
Dim i As Integer
Dim j As Integer

'Specify folder where files are located
PathToUse = "C:\Batch Folder\"
'Count files in folder
OldName = Dir$(PathToUse & "*.doc")
While OldName ""
i = i + 1
OldName = Dir$()
Wend
'Rename files
j = 0
myFile = Dir$(PathToUse & "*.doc")
Do While myFile "" And j i
j = j + 1
Set myDoc = Documents.Open(FileName:=PathToUse & myFile, Visible:=False)
With myDoc
OldName = .FullName
Set oRng = .Words(1)
oRng.End = .Words(min(9, .Words.Count - 1)).End
NewName = Trim(oRng.Text) & ".doc"
NewName = Replace(NewName, "\", "")
NewName = Replace(NewName, ":", "")
NewName = Replace(NewName, """", "")
NewName = Replace(NewName, vbCr, "")
NewName = Replace(NewName, vbTab, "")
.Close SaveChanges:=wdSaveChanges
End With
Name OldName As PathToUse & NewName
myFile = Dir$()
Loop

End Sub
Private Function min(a As Long, b As Long)
min = -((a b) * a + (a = b) * b)
End Function


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

catmanbaloo wrote:
I recently had to recover a great number of files, but unfortunately
although all the information is in the word documents, the titles are
all file01, file02 etc. I was wondering if there exists a tool that
would allow me to rename the files based on the first few words of
the document? This would save us many hours of boring detailed work.



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically turn Hyperlinks into text nico Microsoft Word Help 2 February 13th 05 07:33 PM
Automatically adding accents when typing in another language? amoretto28 Microsoft Word Help 1 February 7th 05 06:08 AM
Can one item of text be automatically replicated in several spots? Chris Microsoft Word Help 1 January 20th 05 03:28 PM
Automatically update style Øyvind Alsos Microsoft Word Help 2 January 14th 05 01:19 PM
How do you REALLY turn off automatically update styles in Word 200 College Student Microsoft Word Help 1 January 8th 05 11:17 PM


All times are GMT +1. The time now is 10:33 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"