PDA

View Full Version : [SOLVED:] Remove specific text from a Word Doc file name



Cainlynk
10-30-2015, 09:55 AM
My apologies if the answer to this is out there somewhere already; I have been searching the internet for a while now without finding a solution so here goes.

I have to go through a list of MS Word documents and remove the word "Partnership" where ever it appears (Most but not all of the filenames contain this word). There are 600+ files and they are all in the same folder. The lengths of the file names are all different, and the word "Partnership" is sometimes sometimes but not always at the end.

Can someone please help me out with VBA code that would loop through each file in the folder and remove the word "Partnership"?

Thanks in advance!

gmaxey
10-30-2015, 11:20 AM
Sub RenameFiles()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object

On Error Resume Next
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("D:\Test") 'Replace with whatever folder holds your files.
For Each oFile In oFolder.Files
oFile.Name = Replace(oFile.Name, "Partnership", "")
Next
lbl_Exit:
Set oFSO = Nothing
Set oFolder = Nothing
Set oFile = Nothing
Exit Sub
End Sub

Cainlynk
10-30-2015, 01:12 PM
Sub RenameFiles()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object

On Error Resume Next
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("D:\Test") 'Replace with whatever folder holds your files.
For Each oFile In oFolder.Files
oFile.Name = Replace(oFile.Name, "Partnership", "")
Next
lbl_Exit:
Set oFSO = Nothing
Set oFolder = Nothing
Set oFile = Nothing
Exit Sub
End Sub



Worked Like a charm!! Thanks so much!!

gmaxey
10-31-2015, 06:35 AM
You're welcome.