Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 08-28-2014, 09:10 PM
macropod's Avatar
macropod macropod is offline Macro to replace SSN in all files within a folder Windows 7 64bit Macro to replace SSN in all files within a folder 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

It would be better to do this when the documents are generated, not afterwards. If they are being generated via mailmerge, for example, field coding can be used to output XXX-XX- plus only the last four digits.

That said, the following macro will process all documents in the selected folder:
Code:
Sub BulkReplaceSSNs()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
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.Range.Find
    .ClearFormatting
    .Text = "([0-9]{3})-([0-9]{2})-([0-9]{4})"
    .Replacement.Text = "XXX-XX-\3"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = 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 includes its own browser, so all you need do is select the folder to process, and let it run. Just be sure to not save the document you're running the macro from to the same folder.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to replace SSN in all files within a folder macro to change name of files in a folder in order expert4knowledge Word VBA 5 07-10-2014 03:54 PM
Remove title property from all files in a folder konopca Word VBA 2 12-04-2012 10:54 AM
Macro to replace SSN in all files within a folder Move files from one folder to another obasanla Word 1 09-28-2012 04:42 PM
Macro to replace SSN in all files within a folder Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM
Adding a field to a folder of PDF files. Balliol Windows 0 11-22-2009 02:02 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:56 PM.


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