3 things I don't get

R

rob p

This is related to another of my posts but I want to keep separate so not to
confuse. I have a number of checkboxes in different cells (one checkbox
only in a cell).

What is selection.cells(1) ? Is this cell # 1 or just the first cell of
possibly more than 1?

Same question: selection.formfields(1)

Same question: selection.formfields.count 1 (or 0) ???

I want one macro that refers to a checkbox by discovering it's (checkbox's)
name or number rather than hard coding the name in - (like
activedocument.formfields("check1").checkbox.value = whatever....) Hard
coding would work I guess but I would need a separate copy of one macro for
each form field with it's own unique form field name.
Thanks.
 
J

Jean-Guy Marcil

rob p was telling us:
rob p nous racontait que :
This is related to another of my posts but I want to keep separate so
not to confuse. I have a number of checkboxes in different cells
(one checkbox only in a cell).

What is selection.cells(1) ? Is this cell # 1 or just the first cell
of possibly more than 1?

The first cell in the current selection.
Same question: selection.formfields(1)

The first formfiled in the current selection.
Same question: selection.formfields.count 1 (or 0) ???

This is wrong. The .Count property returns the number of item in a
collection. In this case, the number of formfields in the selction, which
might be different from

ActiveDocument.FormFields.Count
which will return the total number of formfields in the document.

I want one macro that refers to a checkbox by discovering it's
(checkbox's) name or number rather than hard coding the name in -
(like activedocument.formfields("check1").checkbox.value =
whatever....) Hard coding would work I guess but I would need a
separate copy of one macro for each form field with it's own unique
form field name.

It depends how the code is invoked. If it is from the OnExit/OnEntry event
of the formfield, then Selection.FormFields(1) will always refer to the
currently active one. No need for names here.

But it is not entirely clear what you are trying to do here... (At least to
me!)
If you can explain more clearly, with some "real" examples, I am sure you
will get good suggestions...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rob Peterson

Here is some of the code. I want to exit a checkbox. If checked, make text
in the cell dark, if not checked then shaded gray. Problem is getting the
name of the form field when there are many in the template / table.
Thanks

If ActiveDocument.FormFields( (THIS NEED TO BE THE NAME)).CheckBox.Value =
True Then
oCell.Range.Font.Color = wdColorBlack
oCell.Range.Font.Bold = True
Else
oCell.Range.Font.Color = wdColorGray50
oCell.Range.Font.Bold = False
End If
 
J

Jay Freedman

When the code is in the checkbox's exit macro, then the checkbox
(which actually has not yet been exited) is Selection.FormFields(1)
and you don't need to know its name. Just change the If statement to

If Selection.FormFields(1).CheckBox.Value = True Then

Referring to your other thread, this will *not* work in the exit macro
of a text form field, because of the bug that text fields "don't
count" in the FormFields collection.
 
R

rob p

That works fine for checkboxs. It's really getting clearer. Thanks.

Now, is there a way to the same with text box depending on whether or not
there was text in it? Or will "bugs" prevent it?
Thanks again.
 
J

Jay Freedman

Hi Rob,

Yes, there's a way. It's more complicated because of the bug, but the
workaround is based on the code you saw before, for using the bookmark
when the formfield index isn't valid.

Sub ExitTextbox()
Dim oFFld As FormField

On Error GoTo ExitTextbox_Error

Set oFFld = Selection.Bookmarks( _
Selection.Bookmarks.Count) _
.Range.FormFields(1)

If oFFld.TextInput.Valid Then
If Len(oFFld.Result) > 0 Then
MsgBox oFFld.Result
Else
MsgBox "It's empty!"
End If
End If

On Error GoTo 0
Exit Sub

ExitTextbox_Error:

MsgBox "Error " & Err.Number & " (" & _
Err.Description
End Sub
 

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