How do I test whether a variable I've "set" actually exists before testing it in a conditional statement?
I have a loop that iterates through every character in a selection range and performs an operation depending on the content of the previous character (for example, if the previous character is an exclamation point). This works great so long as the selection doesn't include the first character of the document, at which point the macro fails and throws a '91' error. How do I test whether a variable I've set actually exists before testing it in a conditional statement, so that I can avoid this error?
Code:
For Each strChar In Selection.Range.Characters
Set strPrevChar = strChar.Previous(wdCharacter, 1)
Set strNextChar = strChar.Next(wdCharacter, 1)
If strPrevChar <> "!" Then
' Perform the operation
End If
Next strChar
(The If statement throws an error when the first selected character is the first character in the document and strPrevChar is not set because there is no previous character.)