Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word 2003: Add a new table row with formfields in a protected form 2

Status
Not open for further replies.

BlueHorizon

Instructor
Jan 16, 2003
730
US
Hi you smart people! Happy Thanksgiving to those of you who celebrate!

I've searched for an answer to this one but I don't understand what I've read.

Novice to VBA. Willing to learn.

Protected form in Word: I have a table with 3 columns and 3 initial rows. Each column contains a formfield so that when the doc is password protected, users tab from field to field to enter data.

Here's my dilemma: How can I get a MessageBox to pop up when the user tabs from the last formfield in the table? I would like the MsgBox to ask the user if they need another row. If they answer Yes, a new row with the same formfields as the row before is added. If they answer No, the tab moves them to the next formfield outside the table.

If this possible? If so, how? I've read that this macro should be "on exit" of the formfield.

Thanks in advance! I appreciate the help, as always!!




Best,
Blue Horizon [2thumbsup]
 



hi,

Your macro will have to UNPROTECT the document, insert a row, add the necessary formfields to that new row, PROTECT the document.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
It ain't that easy. Yes, Skip is correct, you have to unprotect to do anything really.

And yes, you can display the messagebox with an OnExit procedure attached to the "last" formfield.

Although what if they do not enter that last one?

Plus, the fact you have more formfield after the "last" one is an issue. Obviously the "last" one is not really the last one, since there are apparently more.

Are your formfields well named?

"If they answer Yes, a new row with the same formfields as the row before is added. "

technically this is not possible as they will be unique. They can NOT be the same. However, they can look like they are the same. BUT....think this out.

If the "last" formfield (in the table) has an OnExit macro asking if you want more (row and formfields), one would assume that if you DO want more, then the NEW "last" formfield should now have that honour. Yes?

Which means...you have to remove the OnExit from its original owner, and assign it to a new formfield.

Can it be done? Yes. But it ain't easy.

Working on a demo file to show you. I (unfortunately) have income-producing work I must do first.




Bummer.


unknown
 
The reason I asked about your naming, is that while possible, it is a BAD BAD idea to copy and paste formfields.

Why? Because the new formfields steal the name of the original and leave the original nameless. It is still there but no longer has a name, and therefore no longer has a bookmark.

So if you ever want to get data OUT of that formfield (via code) can be a right darn-nabit pain.


unknown
 
It will be something like:
Code:
Sub More_OR_Not()
Dim msg As String
Dim aCell As Cell
Dim aRow As Row

[COLOR=red]' set up message[/color red]
msg = "Do you want a new row of fields?" & vbCrLf

[COLOR=red]' if yes, unprotect, collapse and
' make new empty row[/color red]
If MsgBox(msg, vbYesNo) = vbYes Then
   ActiveDocument.Unprotect
   Selection.Collapse 0
   [COLOR=red]' yes believe it or not
   ' this makes a new row[/color red]
   Selection.MoveRight Unit:=wdCell, Count:=1
Else
   Exit Sub
End If

[COLOR=red]' set row object[/color red]
Set aRow = Selection.Rows(1)
[COLOR=red]' put new formfields in each cell of new row[/color red]
For Each aCell In aRow.Cells
   aCell.Range.FormFields.Add _
      Range:=aCell.Range, _
         Type:=wdFieldFormTextInput
Next

[COLOR=red]' make the "last" cell formfield get this procedure
' to make new row/formfields[/color red]
Selection.Tables(1).Range _
   .Cells(Selection.Tables(1).Range.Cells.Count) _
      .Range.FormFields(1).ExitMacro = "More_OR_Not"

[COLOR=red]' make the previous formfield with this procedure
' now have NO OnExit[/color red]
Selection.Tables(1).Range _
   .Cells(Selection.Tables(1).Range.Cells.Count - 3) _
      .Range.FormFields(1).ExitMacro = ""

[COLOR=red]' reprotect the document[/color red]
ActiveDocument.Protect wdAllowOnlyFormFields, _
   NoReset:=True, Password:=""
End Sub


unknown
 
NOTE: the three new formfields are:

text formfields (as you did state which, I made an assumption)

and their names (bookmark) will be Textx, with x being the next value of the LAST default named formfield.

So if the last default named formfield was Text9, the three new ones will be:

Text10
Text11
Text12

Can they be explicitly named? Yes.


unknown
 
Oh, and the removal instruction for the previous formfield OnExit is, as you can see, hard-coded. Just to make posting this quick. You stated 3 columns, so counting back from the last cell, .Count - 3.

Could this be dynamic? Yes. Take a bit more coding though.

Ta-ta.


unknown
 
Hi fumei,

This is fantastic! Works like a charm! Thank you for sharing your expertise...wish I knew what you know....

One more question: What if I have the form password protected? What code would unprotect with a password and then reprotect with the same password and where would that go in the code you gave me?

Thanks!!!

Best,
Blue Horizon [2thumbsup]
 
What if I have the form password protected? What code would unprotect with a password and then reprotect with the same password and where would that go in the code you gave me?
Add & define a new variable to hold the password, then use it where the code unprotects & reprotects the document, thus:
Code:
Dim Pwd as String
Pwd = "Your password"
...
ActiveDocument.Unprotect Password:=Pwd
...
ActiveDocument.Protect wdAllowOnlyFormFields, _
NoReset:=True, Password:=Pwd


Cheers
[MS MVP - Word]
 
Thank you, fumei! That works beautifully!

Happy Holidays to you and all Tek-Tippers!

Best,
Blue Horizon [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top