Consulting

Results 1 to 4 of 4

Thread: Remove specific text from a Word Doc file name

  1. #1
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    3
    Location

    Remove specific text from a Word Doc file name

    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!

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    3
    Location

    Talking

    Quote Originally Posted by gmaxey View Post
    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!!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    You're welcome.
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •