Error check for cursor position

J

JuanManuel

Is there any code to make the following work:

Private Sub RadBtnTMS1_Click()
'if
'cursor is placed in an empty line within a cell then
ActiveDocument.TrackRevisions = True
'else
Exit Sub
End Sub

I want that, upon the selection of a radio button, the program enables track
changes IF and ONLY IF the cursor is placed in a line with no text.

Thank you,

Juan
 
J

Jean-Guy Marcil

JuanManuel said:
Is there any code to make the following work:

Private Sub RadBtnTMS1_Click()
'if
'cursor is placed in an empty line within a cell then
ActiveDocument.TrackRevisions = True
'else
Exit Sub
End Sub

I want that, upon the selection of a radio button, the program enables track
changes IF and ONLY IF the cursor is placed in a line with no text.

Try this:

Private Sub RadBtnTMS1_Click()

Dim rngLine As Range

Set rngLine = Selection.Bookmarks("\Line").Range

If AscW(Trim(rngLine.Text)) = 11 Or AscW(Trim(rngLine.Text)) = 13 Then
'cursor is placed in an empty line within a cell then
ActiveDocument.TrackRevisions = True
Else
Exit Sub
End If

End Sub


If spaces are not considered empty spaces, remove the "Trim()" function.
Also, this code does not check for a line that would have only tab
characters or a mix of spaces and tab characters...
 
J

JuanManuel

Jean-Guy,
Thank you very much for your help. Nevertheless, I am testing the following
code (pretty much the same that you provided) and it is always going through
the "else".
Private Sub RadBtnTMS1_Click()

'==================== CODE ==========================
Dim rngLine As Range

Set rngLine = Selection.Bookmarks("\Line").Range

If AscW(Trim(rngLine.Text)) = 11 Or AscW(Trim(rngLine.Text)) = 13 Then
'If cursor is placed in an empty line within a cell then
ActiveDocument.TrackRevisions = True
Else
RadBtnTMS1.Value = False
'Unselect the radio button

MsgBox "Cursor needs to be placed in a new line before selecting a
TMS"
Exit Sub
End If

End Sub

'==================== END OF CODE ======================

Even when I place the cursor in a new line of the document, it is displaying
the message box.

What could be going on?

Thank you, Juan
 
J

Jean-Guy Marcil

JuanManuel said:
Jean-Guy,
Thank you very much for your help. Nevertheless, I am testing the following
code (pretty much the same that you provided) and it is always going through
the "else".

Check your document and the lines you are testing the code against.

I just tested your code "as is" on lines containing nothing but a hard line
break (¶), a bunch of space ended by a ¶, nothing but a soft line break
(SHIFT-Enter) or a bunch of spaces followd by a SHIFT-Enter.
In all cases it turn tracking on, it never went to the message box.
 
J

JuanManuel

Thank you Jean-Guy. It is actually working great within a table.

Nevertheless, when I try it in an empty line outside any table, sometimes it
shows me an error that says: Run-time error '5': Invalid procedure call or
argument.

More specifically, the error only occurs when I've placed the cursor in the
last line allowed by the document (i.e., the last line where you can place
the cursor with the click of a mouse). Also when I've placed the cursor in
the last line allowed and press enter to create a new available empty line
and the cursor moves to it, the error shows up upon pressing a button.

The same button works perfectly within cells of a table regardless of where
I place the cursor and whether or not it is placed in a recently created
empty line.

What could be going on?

Thank you.
 
J

Jean-Guy Marcil

JuanManuel said:
Thank you Jean-Guy. It is actually working great within a table.

Nevertheless, when I try it in an empty line outside any table, sometimes it
shows me an error that says: Run-time error '5': Invalid procedure call or
argument.

More specifically, the error only occurs when I've placed the cursor in the
last line allowed by the document (i.e., the last line where you can place
the cursor with the click of a mouse). Also when I've placed the cursor in
the last line allowed and press enter to create a new available empty line
and the cursor moves to it, the error shows up upon pressing a button.

The same button works perfectly within cells of a table regardless of where
I place the cursor and whether or not it is placed in a recently created
empty line.

What could be going on?

The "\Line" preset bookmark does not include the last ¶ of the document.

So, when used on the last line, if that last document line is empty, the
Trim function removes all spaces and you are left with an empty string...
AscW("") causes the error...
Modify your code like this:

If Trim(rngLine.Text) = "" Then
MsgBox "This is the last line of the document and it is empty."
'Do what you have to do in this case...
ElseIf AscW(Trim(rngLine.Text)) = 11 Or AscW(Trim(rngLine.Text)) = 13 Then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top