I'm currently struggling with a very simple issue.
For a macro, I'd like to know if my cursor is on an empty line or not. I thought to use Len()
or Count
but they always return 1
(even if there are more characters on the current line).
I assume that using a Range
rather than Selection
could help but I have no idea how to do it (I'm not very skilled with vba-word).
I guess I could select the whole line (how?) and then if Selection.Characters.Count > 1 Then [...]
but it seems ugly and unefficient.
Thanks in advance for any help or piece of advice,
Try this ...
If Asc(ThisDocument.Characters(Selection.Start)) = 13 And Asc(ThisDocument.Characters(Selection.Start + 1)) = 13 Then
MsgBox "y"
End If
ThisDocument.Characters
contains all the text in the document, split into individual characters (eg letters/digits/spaces/etc)
Asc(...)
gets the character code for a particular character
13
is the code for a carriage return (new line)
Selection.Start
refers to the character before the caret/cursor
Selection.Start + 1
refers to the character after the caret
As advised by @Ezor, this may not work on all versions of Word VBA, so an alternative would be to use ActiveDocument
rather than ThisDocument
, eg
If Asc(ActiveDocument.Characters(Selection.Start)) = 13 And Asc(ActiveDocument.Characters(Selection.Start + 1)) = 13 Then
MsgBox "y"
End If