Saving data in several form fields to .txt file

S

Solsen

How can I save information in the fields of a from
document I sent to several people, into one .txt file?
 
P

Peter Jamieson

Do you have several different form documents, or are you filling in the
form, sending it, filling in different details, sending the form, etc.?

In any case, you're probably going to need a macro to do this as the
built-in method of saving form data cannot append data to an existing .txt

You could use something like the following (but you'd need to test it and
add error handling code):

Sub SaveFormData()
Const sDataFilePathName = "c:\a\mydata.txt"
Dim sOutputLine As String
Dim oFormField As FormField
Open sDataFilePathName For Append As #1
s = ""
For Each oFormField In ActiveDocument.FormFields
If s <> "" Then s = s & ","
Select Case oFormField.Type
' At the moment the processing its the same but you
' might want to change e.g. how you output the
' value of a checkbox
Case wdFieldFormTextInput
s = s & """" & DoubleUp(CStr(oFormField.Result)) & """"
Case wdFieldFormDropDown
s = s & """" & DoubleUp(CStr(oFormField.Result)) & """"
Case wdFieldFormCheckBox
s = s & """" & DoubleUp(CStr(oFormField.Result)) & """"
Case Else
s = s & """?"""
End Select
Next
Print #1, s
Close #1
End Sub

Function DoubleUp(ByRef s As String) As String
' doubles up any double quotes in the passed string
Dim i As Integer
DoubleUp = ""
For i = 1 To Len(s)
If Mid(s, i, 1) = """" Then
DoubleUp = DoubleUp & """"""
Else
DoubleUp = DoubleUp & Mid(s, i, 1)
End If
Next
End Function
 

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