PDA

View Full Version : Want to save the formfields after refreshing the document



madhavi
09-17-2010, 03:22 AM
*** Can we assign Data into Fromfield Default Value Property Dynamically in msword 2003? how?

macropod
09-17-2010, 04:50 AM
Hi madhavi,

Your question is ambiguous, but I believe you want to know how to programmatically update a FormField. You can do it with code like:

Sub UpdateFormField(FFName As String, FFText As String)
Dim Pwd As String
Pwd = ""
With ActiveDocument
.Unprotect Password:=Pwd
.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd
End With
End Subwhere the calling routine passes both the formfield's name (FFName) and content (FFText) as parameters. Note that, if your form is password-protected, you'll also need to supply the password between the double quotes on the line:
Pwd = ""

Tinbendr
09-17-2010, 09:30 AM
Welcome to VBA Express.

Also

ActiveDocument.FormFields(1).TextInput.EditType Type:=wdRegularText, Default:="testing", Format:=""

madhavi
09-20-2010, 05:18 AM
for protection and unprotection we wrote below code:


If Formulier.Mode_Formulier.Value Then
' ThisDocument.Bookmarks("Start").Select
ActiveDocument.Unprotect
ThisDocument.Bookmarks("Veld1").Select
ActiveDocument.Protect wdAllowOnlyFormFields
ThisDocument.Bookmarks("Veld1").Select

' ThisDocument.Bookmarks("Start2").Select
Else
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("bkmStart").Select
Selection.MoveDown Unit:=wdScreen, Count:=3
Selection.MoveUp Unit:=wdScreen, Count:=4
End If
End Sub

for updating the fields we wrote below code:


Private Sub UpdateFields()
For Each objField In ActiveDocument.Fields
objField.Select
objField.Update
Debug.Print objField.Code.Text & vbTab
Next objField
(HERE BEFORE Objfiled.Update I NEED SOME CODE THAT SHOULD AVOID REFRESHING THE FORMFIELDS)
ENDSUB

macropod
09-20-2010, 05:38 AM
Hi Madhavi,

My previous post (and Tinbendr's) showed you how to update a formfield programmatically. My post also showed how to unprotect a document, then re-protect it without losing the formfield contents.

Your code suggests there is a more basic issue to be dealt with. Perhaps you could explain what you are trying to do and why you are unprotecting the document and running the 'UpdateFields' macro.

You also need to look at coding more efficiently - there should be no need for any of your Select statements, much less the MoveDown and MoveUp commands.

fumei
09-20-2010, 09:44 AM
macropod, why do you use Bookmarks when dealing with formfields?

.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText


instead of:

.FormfFields(FFName).Result = FFText
I understand that the result will, in fact, come out the same, but it seems to me that if the object you want to action is a formfield, then use...the formfield itself.

fumei
09-20-2010, 09:55 AM
madhavi:


Private Sub UpdateFields()
For Each objField In ActiveDocument.Fields
objField.Select
objField.Update
Debug.Print objField.Code.Text & vbTab
Next objField
' (HERE BEFORE Objfiled.Update I NEED SOME CODE THAT SHOULD
' AVOID REFRESHING THE FORMFIELDS)
ENDSUB


1. since you do not appear to be declaring objects, it looks like you are not using Option Explicit. I strongly recommend you start doing so.

2. avoid selecting things, especially if there is absolutely no need to do so.

3. if you do not want to process formfields..then do not process formfields:

Sub UpdateAllFieldsEXCEPTFormFields()
Dim aField As Field

ActiveDocument.Unprotect
For Each aField In ActiveDocument.Fields
If aField.Type <> wdFieldFormTextInput Then
aField.Update
End If
Next
ActiveDocument.Protect wdAllowOnlyFormFields, _
noreset:=True, Password:=""
End Sub
This ignores text formfields. Or more accurately, if the Type of field is NOT a textinput formfield, then Update it.

macropod
09-20-2010, 03:18 PM
macropod, why do you use Bookmarks when dealing with formfields?Hi Gerry,
Because the bookmarks approach isn't limited to 255 characters!

madhavi
09-21-2010, 12:46 AM
4546Hi Macroopod & Tim ,
Good morning,
Thank you very much for your valuble suggestions and answers.


you asked me why i am running "UPDATE" macro, since we have a requirement like we need to fill capture the information from user in form of formfields and docproperties and need to update it in the document.

but unfortunately we are not able to capture the user given data in formfileds .becoze of that i need some code to avoid freshing the formfields only.

i am sending the sample document which i am working with and also sending the instructions of the functionality.
please check the attachment i am sending for better understanding.


Every little assistance is highly appriciated

macropod
09-21-2010, 01:02 AM
If you're updating docproperty fields programmatically, and you want to pick up those values in the body of the document, you don't need formfields (or document protection) for that unless you also want users to be able to edit those fields whilst still preserving the ability to change the same properties again at a later time - all you need is DOCPROPERTY fields pointing to the relevant DOCPROPERTY entries.

Gerry (fumei) has also posted code showing how to safely update the various fields without affecting the formfields. I suggest you study the various macros you've been provided with, to see how to adapt them to your own project.

madhavi
09-21-2010, 05:47 AM
Hi All ,

I came to know what my client wants actually.
when we double click on form fields we will get "Text form field options"pop up window.
In that we have default text option .
and i need some code for: the data given by user in formfield should get updated in default text of that formfield .

please check step 3 the attachment for more details.




thank you in advance

fumei
09-21-2010, 08:54 AM
1. "when we double click on form fields we will get "Text form field options"pop up window."

Only when the document is UNPROTECTED.

So let me see if I understand. When you the command button is clicked you want the default text of the text formfields to change to whatever is the current text value of that formfield. Yes? If this is correct, then please say that.

2. there is no relation between actioning the formfields and the DOCPROPERTY fields. Other than the fact you are using fields.Update. In fact, if what you want to do is change the default text of a textformfield to whatever the user typed in the formfield (which seems to me to be a very BAD idea), then you can do that without affect the DOCPROPERTY fields at all, or Update any fields.

fumei
09-21-2010, 08:57 AM
(Note: protection mode means please don’t use Tools  protect document)
(Note: to protect the document goto View  Toolbar  Forms , you will get below screen)
I do not know why you make the comment about "please don't use Tools > Protect document.

The button on the toolbar does precise, exactly, the same thiing.

View > Toolbars > the lock button = Tools > Protect document.

They are identical.

fumei
09-21-2010, 09:06 AM
If what you appear to be saying: I want to make the default text the current value of the text formfield, then:
Dim aField As FormField

For Each aField In ActiveDocument.FormFields
If aField.Type = wdFieldFormTextInput Then
aField.TextInput.Default = aField
End If
does that. However, if you Tab through the formfields (while protected) the text showing will NOT be the default text unless you DO perform a refresh.

While I still think changing default text is a poor idea, perhaps if you explain what you REALLY want, this can be finished.