![]() |
#1
|
|||
|
|||
![]()
I am looking for help creating a macro that will replace all social security numbers in each document in a folder. Search string would be "???-??-" to be replaced with "XXX-XX-" so that we keep the last 4 of the SSN. I need to be able to run the macro against a folder of multiple documents with each document containing multiple instances of the SSN to be replaced. I have found some solutions on-line in various places but this is all new to me. I am not a programmer! We currently spend about 2 hours a day manually replacing all of the SSN's in each document.
THANK YOU SO MUCH!!! |
#2
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
This code worked beautifully! Thank you so much!
|
#4
|
|||
|
|||
![]()
I have a word doc with a macro that will search all files in a folder and strip the SSN's from the files (Thanks, macropod!). Now, to make this operation as simple as possible for my customer, I would like to set it up so the macro (VB) is saved to a specific document not globally (I have this part done). What I need help with is configuring that macro to run automatically when the file is opened. End goal is for the customer to open the word doc containing the macro and it auto runs instead of them having to navigate through Word to View/Macros/select and run the macro. I would also like for the word doc to close when the macro is finished running. It will be much easier to distribute the doc with the embedded macros than to instruct each customer how to import the macro.
The script is avail here if needed: https://www.msofficeforums.com/word-...in-folder.html |
#5
|
|||
|
|||
![]()
I figured it out... just renamed the macro to "AutoOpen ()". Now to figure out how to auto close the document after it runs...
|
#7
|
||||
|
||||
![]()
After:
Application.ScreenUpdating = True insert: ThisDocument.Close SaveChanges:=True Note: using SaveChanges:=True ensures any edits you've made to the document and/or its code will be saved. PS: I've merged your threads into one, since they all concern the one project.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]()
Thanks again macropod!!
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
obasanla | Word | 1 | 09-28-2012 04:42 PM |
![]() |
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 |