Find Replace Multiple Macro in Word - vba

I want a quick way to deidentify documents by removing employees' signatures in comments (yes, I already have one that removes identifying info, just not the text itself). A macro that could search all employee names and replace with "" would be great. I'm sure there is a simple way to do this.
Everything I've tried has failed, either not finding text in comments or not working after I copy/paste the recorded code for Find/Replace one name and adjust for the other 20-30 names. I've tried probably 4 different find/replace codes that work for the main text, but none have worked for the comments.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Employee Name 1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Employee Name 2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Copying/pasting this through all 20-30 employees fails (no error, just doesn't replace). I'm sure there's a more elegant way to do it anyway, but I'm definitely not familiar enough with code.

It's not 100% clear from the phrasing in the question whether code for removing the comments' author and initials is also required, or just text in the comments that refers to employees' names. So the code below does both.
First, an array is declared and populated with the employees' names that should be removed. This will make it easier to manage the list and shorten the amount of code (doesn't need to be copy/pasted for each employee).
Then, the collection of comments is looped and two tests are perfomed.
Does the comment text contain any employee's name. To determine this, the array of employee names is looped and the presence of the name checked using Instr. If a name is present, Find/Replace is run on the Comment.Range. (Note: this code contains only the barebones Find properties, you may need to add some things if more is needed.)
Remove Authorand Initial information. Here, the list of author names is part of the Case test. If a comment contains one of them, the Author and Initial information is set to an empty string.
Note that it comment's Range does not include the comment's author or initials information, only the content of the comment (which you may know, but others reading this may not).
Sub RemoveAuthorFromAllComments() ' TestLoopCommentWords()
Dim C As Word.Comment, rng As Word.Range
Dim employees() As Variant, e As Variant
employees = Array("Cindy Meister", "John Doe", "Mary Jane")
For Each C In ActiveDocument.Comments
Set rng = C.Range
For Each e In employees
If InStr(rng, e) <> 0 Then
With rng.Find
.Text = e
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End If
Next
Select Case C.Author
Case "Cindy Meister", "John Doe", "Mary Jane"
C.Author = ""
C.Initial = ""
Case Else
Debug.Print C.Author
End Select
Next
End Sub

Related

Find and Replace multiple values form a string and replace with a word

I'm looking into creating a macro that would search for a set of many king o texts variations and change it to something else.
For examples:
Find a paragraph followed by the word Art1, Art1., etc = ˆpArt1, ˆpArt1., ˆpArt 1, ˆpArt I, so on... replace with the string ˆpArt. 1. in bold.
So basically, I have many variations of an occurrence that I need to change so a specific text, and I would be constantly feeding this string to look for many variations on the search to replace with something I would define.
Any ideias?
this is what I'm trying with no success:
Sub Macro1()
'
' Macro1 Macro
'
'
Dim Arts1 As String
Arts1 = Split(("^p1o", "^p1 o")
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Arts1
.Replacement.Text = "^pArtigo 1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
You could use the .replace function for the string.
"^pArt1 Art1".replace("Art1", "TODO")

How to Find a Specific Keyword from the beginning of the Word Document in VBA?

I am using Word VBA. I want to find a specific keyword "MyTest" from the beginning of the document, and then repeat until all of occurrences are found. How to do so?
I use macro record, and get the following codes:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "MyTest"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
This seems only start the find from the current position and will return one instance of the keyword?
The macro recorder will not give you the best code as it can only record what you do on screen. This means that it always works with the Selection object, i.e. whatever you have selected on screen.
Instead you should use a Range object set to the the part of the document you want to work with. Unless you are using ReplaceAll you also need to repeatedly execute the Find until you have found all the matches.
Below is a generic routine that you can modify.
Sub FindSomeTextAndDoSomething(textToFind As String)
Dim findRange As Range
Set findRange = ActiveDocument.Content
With findRange.Find
.ClearFormatting
.Text = textToFind
.Replacement.Text = ""
.Wrap = wdFindStop
.Format = False
Do While .Execute = True
'add code here to do something with the found text
'collapse range to continue
findRange.Collapse wdCollapseEnd
Loop
End With
End Sub

How to add text to empty bullet points in Microsoft Word with VBA?

I am trying to add text to empty formatted bullet points in a word document, however I can't seem to find any successful way of doing so. I'm not very good at VBA, I just use it to automate reoccuring reports.
This is the format of VBA subs I've been using to find and replace text, I just can't find a way to adjust for adding to bullet points:
Private Sub FixedReplacements()
Dim Rng As Range
Dim SearchString As String
Dim EndString As String
Dim Id As String
Dim Link As String
Set Rng = ActiveDocument.Range
Rng.Find.ClearFormatting
Rng.Find.Replacement.ClearFormatting
With Rng.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Rng.Find.Execute Replace:=wdReplaceAll
End Sub
The goal I have set for empty bullet points is for them to display something along the lines of "No further information." << Just an example.
How this can be done depends very much on how the bullets were inserted. There is no way to specifically search the bullet, itself. A comment mentions
bullet points are the formatted bullet points found straight out of
word.
In that case, the default setting would be to format the paragraphs with the List Paragraph style. If that's the case, here, then Find can search for paragraphs using that style. The Find code in the question would then look as follows. (Note also the changes to the Format and Wrap properties.)
With Rng.Find
.Text = "^p"
.Replacement.Text = "No further information.^p"
.Forward = True
.Style = "List Paragraph"
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If a different style has been used to apply bullets, then that style name can be searched. Keep in mind that style names are case-sensitive.
There is an option in File/Options/Advanced, Editing options section: Use Normal style for bulleted or numbered lists. If this has been activated then things become very difficult. Best you can do is to try to match the applied paragraph formatting (indents, etc.).

Search word document for month/date and replace with different month/date

In my new job I'm sending invoices with personalized, dated letters. Each month I go into a word doc for each client and update the date from whatever date we last send an invoice to the date we're sending the current invoice. I know basically nothing about coding, but I have managed to get something together that will search for a month and replace it with a new month. I did this by googling and piecing it together. I have no idea if its good or not, but it works.
Sub Test()
With Selection.Find
.Text = "April"
.Replacement.Text = "May"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
My question is, is there a way to search for a date like "Month DD, YYYY" and replace it with a date of my choosing? Also, it won't necessarily be the same previous date.
Thanks in advance
Actually, you don't even need VBA if you don't want to use it --- a wildcard search will do.
Open the Replace dialog, hit More (if necessary), then check Use wildcards.
In the "Find what" box, put MMMM[ ^s][0-9]{1,2},[ ^s][0-9]{4,4}, but with the actual month you want in place of MMMM. It's case-sensitive.
In the "Replace with" box, put the new date.
Hit Replace All.
Bingo! E.g., if MMMM=March, any date in March will be replaced with the new date you specify.
Explanation
[ ^s] is a space or nonbreaking space
[0-9]{1,2} is either one or two ({1,2}) digits ([0-9]).
Similarly, [0-9]{4,4} is exactly four digits.
VBA
For completeness, here it is:
Option Explicit
Option Base 0
Sub Test()
With Selection.Find
.Text = "March[ ^s][0-9]{1,2},[ ^s][0-9]{4,4}" ' ###
.Replacement.Text = "April 1, 2018"
.Forward = True
.Wrap = wdFindContinue
.Format = False
'.MatchCase = False ' ### Not relevant when using wildcards
.MatchWholeWord = False
.MatchWildcards = True ' ###
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
PS- Congratulations on starting out in VBA! Your code looks fine to me - it's pretty close to what you'd get from the macro recorder for the same operation, but cleaner. Two tips you don't need just yet :) — always use Option Explicit at the top of every module, and Dim your variables with specific types.

Microsoft Word macro for formatting selected text, such as italics for all instances of e.g

this is my first question,
I have to go over a large number of documents and make sure that several formatting issues are correct. An example of such an issue would be to make sure that all periods, ".", are not bold, italics, underline, etc. Another example would be to make sure that all "etc." are in italics.
I have a list of the needed formatting issues.
Instead of going over each document and using the find/replace function I would rather write a macro that I can apply to each document.
I have no experience with VBA. I do on the other hand, have some experience with programming in C sharp and C in general.
Any help would be greatly appreciated.
BTW, I'm not asking for a complete program, rather a sample from which I can learn and continue with my own.
There are a couple options:
1.Under the Developer tab in Word, you can hit the "Record Macro" button and do a find and replace multiple times while recording the macro using the ctrl + H shortcut.
2.Have multiple smaller macros setup (such as the two below) hit the "Record Macro" and run them in the order that you want.
Sub ItalicizeEct()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = "ect."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sub RemoveBoldPeriods()
Dim PunctAllRng As Word.Range
Set PunctAllRng = ActiveDocument.Range
With PunctAllRng.Find
.Format = True
.Text = "."
.Font.Bold = True
.Replacement.Text = "."
.Replacement.Font.Bold = False
.Execute Replace:=wdReplaceAll
End With
End Sub
3.Or simply write a large macro that will go through all the editing processes you need