Search and Replace Multiple Words - vba

I already have the code to do a search and replace in Microsoft Word using a VBA macro, but at the moment, my code is copying and pasting the same function multiple times.
What I would like to do is have an dictionary of words that need to be replaced, so for example:
old_word: new_word
Then I have the VBA script to loop through and make the necessary changes in the document.
My code for now is and repeats for each new word pair.
Sub change_words()
With Selection.Find
.Text = "optimize"
.Replacement.Text = "optimise"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "utilized"
.Replacement.Text = "utilised"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Many thanks in advance

Sub change_words(ByVal findWord, byVal replaceWord)
With Selection.Find
.Text = findWord
.Replacement.Text = replaceWord
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
If you change your function to accept parameters as shown, you can call it in a loop, and (for example) pass in the values you want like so:
Dim myDict : Set myDict = CreateObject("Scripting.Dictionary")
myDict("optimize") = "optimise"
myDict("utilized") = "utilised"
For myLoop = 0 to myDict.Count - 1
change_words myDict.Keys()(myLoop), myDict.Items()(myLoop)
Next
That should allow you to reuse the code without repeating it.

Related

Replacing a beacon by a Bookmark in word using excel vba

I am developping an excel macro and i need to replace a specific word (beacon) in my .docx by a bookmark, i found this on the microsoft site.
Sub BMark()
' Select some text in the active document prior
' to execution.
ActiveDocument.Bookmarks.Add _
Name:="tableauxvdd", Range:=Selection.Range
End Sub
But i dont know how to fix the range, i have the idea to select the word with smthg like that :
With word_fichier.Application.Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="#tableauxvdd"
End With
But it need a range and not a selection.
For example:
With word_fichier.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#tableauxvdd"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then .Bookmarks.Add Name:="tableauxvdd", Range:=.Duplicate
End With
Note: As I commented in your other thread, and as demonstrated above, there is no need to use 'Selection' at all for what you are doing.

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

Capitalise the second character in a string while finding and replacing?

extreme VBA noob here. I'm trying to write a macro that searches for every tab and the letter following it, and replaces it with a tab and that capitalised letter.
I've figured out the following using internet articles around the place, but I'm not sure how to correctly write the Replacement.Text line. Thanks in advance.
Sub Capitaliser()
With Selection.Find
Text = "^t?"
Replacement.Text = UCase(Text)
Forward = True
Wrap = wdFindContinue
Format = False
MatchCase = False
MatchWholeWord = False
MatchWildcards = True
MatchSoundsLike = False
MatchAllWordForms = False
Execute Replace:=wdReplaceAll
End With
End Sub
Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^t[a-z]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
.Text = UCase(.Text)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub

How to store italic text into a variable using VBA

Am a beginner in VBA. I need to store only italic text from a specific paragraph into a variable. How to do it?
you can use the following example to look for a text range with the desired formatting and then process them by storing them in the variable used each time you can append the new text with the previous content of the variable.
Eg:
selection_range.Style = _
ActiveDocument.Styles("Text_Format_To_Search")
where
Text_Format_To_Search will be replaced by the text formatting you need to search.
reference from:- http://www.vb-helper.com/howto_format_code_in_word.html
Thanks to KazJaw. Finally I got the solution and I have pasted below
sub test()
ActiveDocument.Paragraphs(2).Range.Select
Selection.Find.Font.Italic = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
str_italic = Selection.Range.Text
End Sub
If you looking to find italicized text and store only the text in a variable, then you can try:
Sub findItal()
Dim italText As String
'Find italic text
With Selection.Find
.ClearFormatting
.Font.Italic = True
.Wrap = wdFindStop
.Execute
If .Found = True Then
italText = Selection.Range.Text
End If
End With
End Sub

VBA word macro / automatically find and replace specific characters for whole document

I'm trying to do the following using the macro:
Upon opening the document, automatically search whole document for brackets {{ }} and delete them including the text inside.
It doesn't do the job well, just operates on the text selected, not the whole document.
Sub SelectToBracketsDelete()
With Selection.Find
.ClearFormatting
.Text = "{{"
.Forward = False
.Wrap = wdFindStop
.Execute
End With
Selection.Extend
With Selection.Find
.Text = "}}"
.Forward = True
.Execute
.Text = ""
End With
Selection.Text = ""
End Sub
Is this what you are looking for?
Word 2007 -> stackoverflow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\{\{*\}\}"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll