Need Macro to insert text form field and protect

J

JFREE223

Looking for a method to insert a text form field and protect a word doc in
one click. Don't know if this is even possible. I have a document that is
generated by an external system. It is populated with several rows of dates
(monthly) and corresponding data. I need to find a method for protecting the
whole document except for a 'column' beside the pre-populated data...on
one-click if possible. The new amount column (not the header) in the table
needs to be unprotected and enterable upon clicking. May not need a form
field. If we could just protect date and Amount upon a click, that may be an
option. Thanks in advance.

example:
Date (pre-populated) Amount (pre-populated) New Amount
1/1/2009 $2,200.00
$3,000
12/31/2008 $2,200.00
$3,050
 
J

JFREE223

I've got the code from 'Insert a Field and assign to button' from
Sarah_Lecturer discussion, which seems to work great. The only thing I'm
missing is being able to insert multiple text form fields instead of one. If
I could highlight the columns in the table and insert all at once...great.
If not, then I'd like it to identify '0' and insert text form field.
 
J

JFREE223

Here is the logic if anybody needs it. This code is searching for "***"

Sub WageProtect()
'
' WageProtect Macro
'
'

' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="fixstyles"
End If

' Insert a Field
ActiveDocument.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput

Dim mySearchString As String
Dim x As Integer
x = 0

mySearchString = "***"

With Selection.Find
.Text = mySearchString
' .Wrap = wdFindContinue
Do While Selection.Find.Execute = True
x = x + 1
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
If x > 10000 Then MsgBox "Possible Endless Loop. The macro has
interrupted itself automatically to prevent freezing the computer.": End
Loop
End With

' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If

End Sub
 
G

Graham Mayor

It could be much simpler! And you haven't reset the password.
Assuming table 1 in the document and that you have a header row.

Sub WageProtect()
'
' WageProtect Macro
Dim oTable As Table
Set oTable = ActiveDocument.Tables(1)
' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="fixstyles"
End If

For i = 2 To oTable.Rows.Count
' Insert a Field
ActiveDocument.FormFields.Add _
Range:=oTable.Cell(i, 3).Range, _
Type:=wdFieldFormTextInput
Next i

' ReProtect the document.
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
Password:="fixstyles", _
NoReset:=True
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JFREE223

Thanks, it would work if we only had one table and it is a better approach
for what I explained. DEFINITELY needed the reprotect code inserted
correctly, so thanks.

We have to search for this in multiple tables and areas in the document.
Text fields have to be inserted where we need a signature, revision to
monetary values, dates, etc...
 
G

Graham Mayor

OK - the following will insert a form field at the cursor (as your original)
and replace each instance of *** in the main document with a text form
field, without going into an endless loop. However it would be probably be
better to identify the ranges where the form fields are intended to go
(multiple tables shouldn't be an issue) and insert the form fields in those
ranges.

Sub WageProtect()
'
' WageProtect Macro
'
' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="fixstyles"
End If
' Insert a Field
ActiveDocument.FormFields.Add _
Range:=Selection.Range, _
Type:=wdFieldFormTextInput
With Selection.Find
Do While .Execute(findText:="***", _
Wrap:=wdFindContinue)
Set oRng = Selection.Range
oRng.FormFields.Add Range:=oRng, _
Type:=wdFieldFormTextInput
Loop
End With
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
Password:="fixstyles", NoReset:=True
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JFREE223

Ah-ha...thanks, but it still seems to insert a form field with the cursor.
This is more simple. Where in the logic do I need to identify not just ***
but also ___-___-____?
 
J

JFREE223

Got it to work...thanks for your help.

JFREE223 said:
Ah-ha...thanks, but it still seems to insert a form field with the cursor.
This is more simple. Where in the logic do I need to identify not just ***
but also ___-___-____?
 
J

JFREE223

Graham Mayor stated that the logic below would insert an intitial field at
the place of the cursor. Is there a way to remove the intitial cursor field
entry using the logic below????

Sub WageProtect()
'
' WageProtect Macro
'
' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="fixstyles"
End If
' Insert a Field
ActiveDocument.FormFields.Add _
Range:=Selection.Range, _
Type:=wdFieldFormTextInput
With Selection.Find
Do While .Execute(findText:="___________", _
Wrap:=wdFindContinue)
Set oRng = Selection.Range
oRng.FormFields.Add Range:=oRng, _
Type:=wdFieldFormTextInput
Loop
End With
' Insert a Field
ActiveDocument.FormFields.Add _
Range:=Selection.Range, _
Type:=wdFieldFormTextInput
With Selection.Find
Do While .Execute(findText:="_______-__-_____", _
Wrap:=wdFindContinue)
Set oRng = Selection.Range
oRng.FormFields.Add Range:=oRng, _
Type:=wdFieldFormTextInput
Loop
End With

' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
Password:="fixstyles", NoReset:=True
End If
End Sub
 
D

Doug Robbins - Word MVP

Remove or comment out the first instance (and possibly the second, depending
upon just what you want to achieve) of the following lines of code:

ActiveDocument.FormFields.Add _
Range:=Selection.Range, _
Type:=wdFieldFormTextInput


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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