How do i edit form field value in MS WORD 2007 - vba

How do i edit Text Form Field (legacy forms) in my document, so that i can then use new value of this field in my VBA script?
I have simple form with Text Form Field (Bookmark = TextFormField1). It is showing default text "default text".
I can access text of this field like this: ActiveDocument.FormFields("TextFormField1").Range.Text
However, if i change value of this field, it seems to delete the field itself, and replacing it with simple text - causing ActiveDocument.FormFields("TextFormField1").Range.Text to throw exception, because TextFormField1 does not exist anymore...
What am i doing wrong? How can i insert text in form field, and use this text in my VBA script?

Try next code:
ThisDocument.FormFields("Index_Or_FormFieldName_or_Bookmarkname").TextInput.EditType Type:=wdRegularText, Default:="Your Form field text"
But it rec the value in to the FormField very slowly.

Use the Result property:
ActiveDocument.FormFields("TextFormField1").Result = "Form field text"

Related

Access 2007 Form : Unbound text box : Memo field truncated

I have a table with a memo field to hold descriptive notes. In a form I have a combo box whose data source query includes this memo field. After update of this combo box I set an unbound text box on the form to the appropriate column number from the combo box.
The memo field is truncated (probably to 255 chars). How can I get the whole memo field to display in the unbound text box?
It is the SQL that is truncating the field. I've tried setting it from a recordset collection but as that's also based on SQL the truncation also occurs.
This seems to be a relatively common problem but I've not been able to find a workaround anywhere.
Thanks in advance for any helpful suggestions...
One method is to use DLookup in the AfterUpdate event of the combobox:
Me!YourTextbox.Value = DLookup("[MemoField]", "YourTable", "ID =" & Me!YourCombobox.Value & "")
assuming the combobox is bound to field ID. Otherwise, use the column of the combobox that holds the ID to look up.

Insert/replace string in existing text form field in Word (VBA)

So I'm trying to create a conditional dropdown in Word. I've used a Legacy Drop-Down Form Field with multiple options.
What I want to happen is when one of the options is selected from the dropdown (and then I guess you have to Tab to enter it...), the Legacy Text Form Field below, that has a simple default text, should populate with a new string from the case statement.
Things that I've already done/got working:
the drop down is working, and on exit it runs a macro
case statement is written out
the result of the case statement is in a variable (string type), let's call it StringVar
can pull the text form field's text (default text) with ActiveDocument.FormFields("TextBox").Result
What I figure out, however, is how to replace the default text in the already existing Text Form Field with the case statement's string variable text.
I've tried
ActiveDocument.FormFields("TextBox").Result = StringVar
but it doesn't change anything inside the text form field. The default text is still there.
So I can answer my own question after pondering and being hit with an "oh, duh" moment.
I had unprotected the document (ActiveDocument.Unprotect Password:="") in order to do this:
ActiveDocument.Content.InsertAfter Text:=("This is my text")
because I was wondering if I was grabbing the right string.
Turns out,
ActiveDocument.FormFields("TextBox").Result = StringVar
DOES work. BUT the file has to be protected (filling in forms) for it to replace the string in the text form field. Otherwise, nothing shows up even though the result had indeed been updated. Fancy that.

Which text field should I use in word and how to fill it

I am trying to use a text field in my word template document but I confused which one should I use?!!
which one you recommend?
Rick text
Text
Text Form Field
My second question is how to address these component in Macro? I use below code:
ActiveDocument.FormFields("TextboxName")
Third question is how to set a value to this component in Macro? so many website use .Value but I don't know why I cannot find this field in above component.
Last question is: if I use any form field, a gray shadow exist below my component, how I can remove it? it is even coming in check print.:(
I am using word 2007.
You would add : textFormField.
you would pass value by Me.FormFields("Text1").Result = "abc"
in your controls tab,
somewhere next to the formField control find the "formFieldShadding"
control. when you select that control your gray shadding will
disappear.

Microsoft Access 07 VBA: Using Text Box or Combo Box as a Control to hide Columns

I currently have a form. The form contains a subform that displays a Query. The Form also has 2 Text Boxes and buttons for both Boxes. The first box acts as a filter for the query to filter specific record. The second box is intended to be used to hide columns in the query. My issue is that my code will not recognize the text box, or any outside source from my form as a Field Name to be found. Here is my current code:
Private Sub Command137_Click()
Forms![Vermont]![Query1 subform].Form.[Query1 Field Name].ColumnHidden = True
End Sub
Currently if I replace "Query1 Field Name" with any field name that exists in the query the column will hide. However if replaced with anything else I recieve the following error:
"Runtime Error '2465'
Microsoft Access Can't Find the Field '|' reffered to in your expression"
I am pretty sure that I am not referencing the Form Control correctly. I have tried replacing [Query1 Field Name] with the following:
[Text142.Text]
[=Text142]
[Text142]
[Forms![Vermont]![Text142]]
I am very new to VBA but I definitely feel as if this is an easy fix; if possible.
Thank you in advance for any help!
Subform references are always a bit weird. This should work, though:
Forms![Vermont]![Query1 subform]![Text142].ColumnHidden = True

Storing a hidden, non-editable variable in a form. Word 97 Compatible

I need a non-editable field in a word form that can be read in vba.
Currently I use a text field, which is hidden. As in:
ActiveDocument.FormFields("DocID").Select
Selection.Font.Hidden = false
//do read then re-hide it.
However users can still 'tab' in to this text field and overwrite the document ID that is there and thus invalidate the vba macro. And unfortunately that is what happens.
I can set the text field property 'Fill-in enabled' to false which gives the desired effect in the form, i.e the user cannot edit it. However now this text field cannot be read in vba.
ActiveDocument.FormFields("DocID").SomeProperty
//throws error 'The requested member of the collection does not exist'
So my question is, is there a way I can store a hidden variable in a word form that can be read in a vba macro?
Unfortunately this has to be a Word-97 compatible solution.
You could use custom document properties instead, see here:
http://msdn.microsoft.com/en-us/library/aa537154(v=office.11).aspx