Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-15-2016, 01:11 PM
Robert K S Robert K S is offline Detecting that previous character doesn't exist (i.e., present character is first in document) Windows 7 64bit Detecting that previous character doesn't exist (i.e., present character is first in document) Office 2003
Novice
Detecting that previous character doesn't exist (i.e., present character is first in document)
 
Join Date: Jul 2016
Location: Cleveland, Ohio
Posts: 10
Robert K S is on a distinguished road
Default Detecting that previous character doesn't exist (i.e., present character is first in document)

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.)
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting that previous character doesn't exist (i.e., present character is first in document) Macro to list all character styles in a document ljd108 Word VBA 8 10-06-2022 01:56 PM
Detecting that previous character doesn't exist (i.e., present character is first in document) Replace a random character with the same character RickLegrand Word 7 07-23-2015 06:35 PM
Detecting that previous character doesn't exist (i.e., present character is first in document) How can select from a specific character to another character mohsen.amiri Word 2 02-19-2015 11:38 PM
Detecting that previous character doesn't exist (i.e., present character is first in document) Regular (roman) character style doesn't change text to roman kcbenson Word 2 10-16-2014 01:31 PM
Detecting that previous character doesn't exist (i.e., present character is first in document) Finding or searching ^ character in word document shahin3121 Word 2 03-05-2012 06:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft