Condividi tramite


Solution - Novice Challenge 2: What Did I See?

This challenge required that you use search and replace and that you know how to call up an input box. Neither of those things is too difficult once you find the right functions and objects. Here’s the solution we came up with:

Sub ChangeSubject()

 

Dim strSubject As String

Dim strReplace As String

 

'Set the string we're going to replace.

strReplace = "<subject>"

 

'Open an input box and read in the string we're going to use as a replacement.

strSubject = InputBox("Enter a subject", "What Did I See Today?")

 

'Set the various Find and Replace settings.

Selection.Find.Text = strReplace

Selection.Find.Forward = True

Selection.Find.MatchWholeWord = True

Selection.Find.MatchCase = True

 

'Replace the found text with the replacement text that was entered in the input box.

Selection.Find.Replacement.Text = strSubject

Selection.Find.Execute Replace:=wdReplaceAll

 

End Sub

 

The first thing we do is declare a couple of variables:

Dim strSubject As String

Dim strReplace As String

 

We’ll be using these variables to store the search and replace strings. And, speaking of search and replace strings, the next thing we do is set our search string:

strReplace = "<subject>"

To complete this challenge we needed to replace all instances of the string <subject> with a subject input by the user. We’ve set our search string to <subject> ; now we need to set our replacement string. But before we can set our replacement string we need to ask the user for the string. We do that by calling the InputBox function:

strSubject = InputBox("Enter a subject", "What Did I See Today?")

We pass two parameters to the InputBox function: the user prompt and the title of the input box. If the user clicks Cancel then the code doesn’t replace the search string. However, if the user clicks OK, the value he or she entered into the input box gets assigned to the variable holding our replacement string, strSubject.

Now that we have the string we want to replace (strReplace) and the string we want to replace it with (strSubject) we can go about doing the actual search and replace. When you do a search and replace from the user interface you need to enter at least two fields: the search string and the replace string. In addition to those two fields, here are other fields you can set through the UI, such as the Match Case option. Those fields (as well as a few others) can also be set using VBA code, as you’re about to see.

You perform a search and replace using the Find object. Before we do that search and replace, however, we’re going to set several properties of the Find object:

Selection.Find.Text = strReplace

Selection.Find.Forward = True

Selection.Find.MatchWholeWord = True

Selection.Find.MatchCase = True

 

Selection.Find.Replacement.Text = strSubject

 

We start by setting the Text property. This is the text we want to find, which, in our case, is contained in strReplace. Next we set the Forward property to True. This means we want our search to go forward through the document. If this were set to False our search would go backwards from our current position.

After that we set the MatchWholeWord property to True. This ensures that we’ll find only the search string we’re looking for. For example, if our search string was “day”, setting MatchWholeWord to False would not only find all occurrences of the word day but would also find daytime, daybreak, Monday, etc.

Another property we set to True is the MatchCase property. As you can probably guess, this means we’re doing a case-sensitive search. Finally, we set the Replacement.Text property to the text we want to insert as a replacement, in this case the string entered by our user and stored in the strSubject variable.

There’s only one final thing left to do: We need to actually perform the search and replace. We do this by running the Execute method of the Find object:

Selection.Find.Execute Replace:=wdReplaceAll

We set the Replace parameter of the Execute method to wdReplaceAll to ensure that the search and replace replaces all occurrences of the search string, not just the first one.

And that’s it for Challenge number 2. Oh, and just in case you’re wondering: when we originally wrote the text for this challenge, what we saw was a duck.

Okay, so maybe you weren’t wondering after all.