Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #18  
Old 11-21-2014, 04:29 AM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

To batch-rename your files, try the following macro. It includes its own folder browser, so all you need to do is to point it to the folder concerned. Just make sure that the only files in that folder are the ones to be renamed. If they don't conform to the structure of your attachment, the macro will crash.
Code:
Sub RenameDocuments()
Application.ScreenUpdating = False
Dim strFldr As String, strDocNm As String, strFile As String, strNewNm As String, wdDoc As Document
Dim FSO As Object, objFile As Object
strDocNm = ActiveDocument.FullName
strFldr = GetFolder
If strFldr = "" Then Exit Sub
Set FSO = CreateObject("Scripting.FileSystemObject")
strFldr = strFldr & "\"
strFile = Dir(strFldr & "*.doc", vbNormal)
While strFile <> ""
  If strFldr & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFldr & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      strNewNm = .SelectContentControlsByTitle("TreatyStatement/OurReference1")(1).Range.Text _
          & "_" & .SelectContentControlsByTitle("TreatyStatement/OurReference2")(1).Range.Text _
          & "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
      .Close SaveChanges:=False
    End With
    If FSO.FileExists(strFldr & strNewNm) Then
      ActiveDocument.Range.InsertAfter "Unable to create:" & Chr(11) & strFldr & strNewNm & Chr(11) & "File Exists" & vbCr
    Else
      Set objFile = FSO.GetFile(strFldr & strFile)
      objFile.Name = strNewNm
    End If
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing: Set objFile = Nothing: Set FSO = Nothing
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
The macro also generates a report of any files it might not be able to rename because a file with the proposed new name already exists. You'll find that report in the document you run the macro from once it's finished processing.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to rename multiple Word file with same suffix ozil61 Word VBA 2 05-06-2014 07:36 AM
How to rename the word doc with the name mentioned in the body of the document How to specify Document Title from the body of my Document SymphonyTomorrow Word 9 08-05-2013 05:31 PM
How to rename the word doc with the name mentioned in the body of the document Rename Document & Save d4okeefe Word VBA 4 05-23-2013 09:35 AM
How to rename the word doc with the name mentioned in the body of the document How to import Word Style Headings and body text into Excel antztaylor Excel 5 11-08-2012 09:54 PM
How to rename the word doc with the name mentioned in the body of the document Advancing Paragraph Numbers in Merge Document Body Wyskers Mail Merge 1 11-30-2011 04:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft