Thread: Batch re-naming
View Single Post
 
Old 06-15-2015, 09:11 AM
twols26 twols26 is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Jun 2015
Location: Canada
Posts: 4
twols26 is on a distinguished road
Post Batch re-naming

How would you revise this macro in order to search for "Policy _________"? Would you be able to search within document and essentially copy and paste "Policy _________" to save as new document name? I actually tried to use a macro (copied below) however I am getting a Runtime error 4198 for .SaveAs FileName:=FirstPara & ".doc".

Help, please

Sub FirstPara()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
Dim FirstPara As String
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
FirstPara = .Paragraphs(1).Range.Text
FirstPara = Left(FirstPara, Len(FirstPara) - 1)
.SaveAs FileName:=FirstPara & ".doc"
.Close
End With
Set wdDoc = Nothing
strFile = Dir()
Wend
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder( 0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by macropod; 06-15-2015 at 04:44 PM. Reason: Split from http://www.msofficeforums.com/word-vba/23610-how-rename-word-doc-name-mentioned-body.html
Reply With Quote