Trouble identifying table cell with insertion point in

C

ChrisC

I have searched for and found code that looks like it ought to work to
do this - here is one example ive tried

Dim IndexCol As Long
Dim IndexRow As Long


With Selection
If Not .Information(wdWithInTable) Then
MsgBox "Selection must be in a table cell.", _
vbCritical, "Invalid Selection"
Exit Sub
Else
IndexCol = .Columns(1).Index
IndexRow = .Rows(1).Index
MsgBox "The coordinates of the top left cell of " _
& "the current selection is row " & IndexRow _
& " and column " & IndexCol & ".", _
vbInformation, "Active Cell"
End If
End With

Now the problem I am experiencing is that my document is locked, and as
such the user cannot select a table row or cell. If I put the cursor
within a cell the wdWithinTable still comes back false, and the
wdEndOfRangeRowNumber etc properties come back as -1.

Answers on a postcard...

Thanks.
 
C

Chuck

You'll need to unlock the document to work with it either programmatically or
manually. If the document is protected using a blank password, then you can
use
ActiveDocument.Unprotect Password:=""
to unprotect the document and then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
to protect it again (the NoReset ensures that your fields aren't wiped clean
- the Type value is up to you, see VBA help for more info).

You can supply the password in those lines of code if it's non-blank but
you'll need to password protect your code if you want to prevent users from
digging in to find the password.

You could also use
ActiveDocument.Unprotect
without the password parameter to prompt the user for a password and then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
to prompt for the password to protect the document again. Unfortunately you
run the risk of users supplying their own or mis-spelled passwords which
could cause problems down the line.
 
C

ChrisC

I have already got code in place to unprotect the document, the user is
able to enter data into the form fields on the form whilst it is
locked, (essentially a questionnaire with form field tickboxes etc),
which is working as intended, but it does mean they cant select a row
in the table, or a cell.
 
D

David Sisson

You could 'break' your doc into sections and only protect the sections
you need.
 
C

Chuck

Next time it would be more helpful if you posted all the code. Also it's
confusing when you say
I have already got code in place to unprotect the document, the user is
able to enter data into the form fields on the form
Those are two separate issues. Unprotecting the document doesn't let the
user enter data into form fields, users can (and usually do) enter form field
data in *protected* documents. You unprotect when you want to change
non-field content or format, or to access certain document properties like
CustomDocumentProperties or other information.

Like I said, if you want your code to manipulate anything other than field
contents or get position information in a protected document, you have to
unprotect it first. If you do in fact have unprotect code in your sub that
unprotects *before* you try to get information like insertion point position,
then there shouldn't be any problem.

Your
 
C

ChrisC

Sorry for the confusion.
From the word go I have been unprotecting the document in the code
before accessing the properties I need, however I have been getting the
results of -1 etc that I have previously described. I have tried
expanding the selection to see if i could get hold of a cell within it
or something but that has also met with failure.

To clarify the point that was confused - the document is always
protected from a users perspective. It only gets unprotected when
changes are being made in the code when a user clicks a button or
something similar.

At the moment as a workaround im having to prompt the user for the row
they are using in the table, which is a bit grim. I dont see why im
having these problems - maybe there is a magic 'allow useful settings
information to work' option?
 
C

Chuck

Like I said, if you want your code to manipulate anything other than field
contents or get position information in a protected document, you have to
unprotect it first.

If you want your code to return information about the insertion point ...

*_you need to unprotect the document first either using code or asking the
user to do it_*

There is no other way. What you are trying to do, get the information while
the document is protected, simply cannot be done.
 

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