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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Form Problem - Getting value from the form into a variable

Status
Not open for further replies.

EliseFreedman

Programmer
Dec 6, 2002
470
GB
Hi There

I am trying to save the values entered into my Word Form into variables so that I can include the variables within an email. I am using the code below
Code:
Dim FrmName As String
Dim FrmClockNo As String
Dim FrmTelNo As String
Dim FrmDept As String


FormName = Selection.FormFields(EmployeeName).Result
FormClockNo = Selection.FormFields(ClockNo).Result
FormTelNo = Selection.FormFields(TelNo).Result
FormDept = Selection.FormFields(Dept).Result
End Sub

However when the code gets to FormName = Selection.FormFields(EmployeeName).Result it is coming up with an error saying the requested member of the collection does not exist.

Does anyone know what I am doing wrong? I think part of the problem might be that when I go to fill in the form field, the form will only let me type to the left of the shaded in form field so I basically end up with [xxxxshaded part] where xxx is the text im typing. Once I type the text, the name of the bookmark disappears. I suspect I need to get it so that the text being typed goes into the shaded part but Im not sure how to do this. Can anyone help.

 
I'd try this:
FormName = Selection.FormFields([!]"[/!]EmployeeName[!]"[/!]).Result

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV

I tried what you suggested but still got the same error
 
And this ?
FormName = ActiveDocument.FormFields("EmployeeName").Result

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Methinks you are confusing formfields and bookmarks. Also, why are you using Selection?
Code:
FormName = Selection.FormFields(EmployeeName).Result
FormClockNo = Selection.FormFields(ClockNo).Result
FormTelNo = Selection.FormFields(TelNo).Result
FormDept = Selection.FormFields(Dept).Result
First of all, that assumes all formfields are selected. If the document is protected for forms (which it needs be to use the formfields), then that is simply not possible. When protected for forms only ONE formfield can be selected at a time.

Which would indeed give that error. Use PHV's suggestion re: ActiveDocument.Formfields

when I go to fill in the form field, the form will only let me type to the left of the shaded in form field so I basically end up with [xxxxshaded part]
That seems very odd. Do you have default text?
Once I type the text, the name of the bookmark disappears.
What does this mean? The name of the bookmark disappears? Is the document in fact protected for forms?

Gerry
 
As an aside, if you have a lot of formfields to work with, you may want to consider using the formfield collection directly, as an object. It makes the code cleaner, and easier to read (shorter).
Code:
[b]Dim DocFF As FormFields[/b]
Dim FrmName As String
Dim FrmClockNo As String
Dim FrmTelNo As String
Dim FrmDept As String

[b]Set DocFF = ActiveDocument.FormFields[/b]

FormName = [b]DocFF[/b]("EmployeeName").Result
FormClockNo = [b]DocFF[/b]("ClockNo").Result
FormTelNo = [b]DocFF[/b]("TelNo").Result
FormDept = [b]DocFF[/b]("Dept").Result

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top