Enable/Disable field fill-in using a macro

  • Thread starter Thread starter Kamran
  • Start date Start date
K

Kamran

I have many tables with a bunch of FORMTEXT fields. When I click an Option
Button, I want it to disable a whole bunch of the FORMTEXT fields (which I
have bookmarked) to avoid mistaken entries. Is there any way I can use the
code below without repeating it 45 times to do this? Following the example
below, the bookmark names are "...T0, T1, T2, T3", etc.

With Selection.FormFields(1)
.Name = "Vent_cLTV1_Vt_T0"
.Enabled = False
End With

Thanks.
 
You could use

Dim i As Variant
Dim k As Variant
Dim sText As String
Dim ofld As FormFields
Set ofld = ActiveDocument.Range.FormFields
For i = 1 To ofld.Count
For k = 0 To ofld.Count
sText = "T" & k
If ofld(i).name = sText Then
ofld(i).Enabled = False
End If
Next k
Next i

If the names of the bookmarks are Vent_cLTV1_Vt_T0,1 etc
Change
sText = "Vent_cLTV1_Vt_T" & k
You may have to limit the count at
For k = 0 To ofld.Count
eg to
For k = 0 to 44
If you have more fields with similar names
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
That's great; thanks, Graham. I'm an amateur with VB so I don't think I
could have ever have come up with that code on my own.
 
Actually, I have one other question:
Is it possible to just have it jump from one field to the next, regardless
of its name, disable fill-in, and do that a number of times? That way I
don't have to code the name, except perhaps the first one. Because I will
have to do this at several points throughout the document.
Thanks again.
 
Just so you get an idea of what I'm looking for, I'm doing something like
this (which isn't working):

"Table_1" is where the 45 fields are that I want to disable.

Dim k As Variant
Dim fieldName As Field

Selection.GoTo what:=wdGoToBookmark, Name:="Table_1"
Set fieldName = Selection.NextField

For k = 1 To 45
Selection.FormFields(k).Enabled = False
Next k

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

Back
Top