Fill text fields with data from txt file - please anyone !

G

gravesen

HI I have a little problem that I would like help with. I have a windo
that opens when I start a template. In this window I can type name an
adresse of the person that I want to send a letter to. I also hav
fields for adding my address. I would like to These fields for my ow
address to be filled with data from a text file so that when I open m
template these data are allready filled. The reason that I would lik
it from a text file is that I would like to make it so that differen
people can have a file with their data if you understand what I mean
Can anyone give me any help on how this can be done??

Thanks a lot
Lars Gravese
 
H

Helmut Weber

Hi Lars,
I think you are talking of a userform and
want to fill some textboxes with text from a textfile.
Just to get you started:
Sub Test198()
Dim iLin As Integer ' a counter for lines
Dim sTxt As String ' the text from a line
Open "c:\test\address.txt" For Input As #1
For iLin = 1 To 5 ' in case there are 5 lines
Input #1, sTxt ' get the text from the line
MsgBox sTxt ' for testing
Next
' or
'Input #1, sTxt
'TextBox1.Text = sTxt
'Input #1, sTxt
'TextBox2.Text = sTxt
'...
Close #1
End Sub
A more elegant way might be, to create an array of textboxes.
But that would no be at a beginners level, I'd say:
Private Sub UserForm_Activate()
Dim i As Integer ' a counter for loop
Dim sTxt As String ' text from a line in file
Dim iTxt As Integer ' number of textboxes
Dim oTxt As TextBox ' object textbox
' get the number of textboxes
For Each oTxt In Me.Controls
iTxt = iTxt + 1
Next
' create array for textboxes
ReDim arTxt(iTxt) As TextBox
iTxt = 0
' assign textboxes to the arrray
' in the sequence they were created
For Each oTxt In Me.Controls
iTxt = iTxt + 1
Set arTxt(iTxt) = oTxt
Next
Open "c:\test\address.txt" For Input As #1
For i = 1 To iTxt
Input #1, sTxt
arTxt(i).Text = sTxt
Next
Close #1
End Sub
It is assumed, that the textfile has got as many lines
as there are textboxes to fill on your userform.
And there are more ways...
 
H

Helmut Weber

....
and if you are talking about a form with
formfields on it, which have to be preset:

Dim iLin As Integer ' a counter for lines
Dim sTxt As String ' the next from a line
With ActiveDocument
.Unprotect
Open "c:\test\address.txt" For Input As #1
For iLin = 1 To 5 ' in case there are 5 lines
Input #1, sTxt ' get the text from the line
.FormFields(iLin).TextInput.Default = sTxt ' fields
Next
Close #1
.Protect wdAllowOnlyFormFields
End With

You may use the names of the formfields instead
of the index. You may also check, if they are of
type wdfieldformtextinput (70).
HTH
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
G

gravesen

HI thanks for your great help.
I am trying to use the following example from you:
------------------------
Dim iLin As Integer ' a counter for lines
Dim sTxt As String ' the next from a line
With ActiveDocument
.Unprotect
Open "c:\test\address.txt" For Input As #1
For iLin = 1 To 5 ' in case there are 5 lines
Input #1, sTxt ' get the text from the line
.FormFields(iLin).TextInput.Default = sTxt ' fields
Next
Close #1
.Protect wdAllowOnlyFormFields
End With
---------------------------------

But I would like to open a form when the dot file is opened. The for
should have some textboxes and the data for some of those should co
from a text file.

I am using this to open my form ontop of the word document:
---------------------
Private Sub Document_Open()
frmInterviewInvitation.Show
End Sub
-------------------------------

Code together yours and mine:
--------------------------------------
Private Sub Document_Open()
Dim iLin As Integer ' a counter for lines
Dim sTxt As String ' the next from a line
With ActiveDocument
Open "c:\test\address.txt" For Input As #1
For iLin = 1 To 1 ' in case there are 5 lines
Input #1, sTxt ' get the text from the line
.FormFields(iLin).TextInput.Default = sTxt ' fields
Next
Close #1
End With
frmInterviewInvitation.Show
End Sub
----------------------------------------------
I have tryed to add your code to the open code but can't get th
textfield filled with the text from my txt file. I am not gettin
errors hmmm perhaps you could give me a hint.


Tnks for the help
Lars Gravese
 
H

Helmut Weber

Hi Lars,
usually you would not open the dot at all,
but create a new document derived from it!
Here are my suggestions, this time very simple,
as it is the beginners group.
I hope the experts don't blame me for it.

You need e.g.
a userform in the dot named frmInterview.
5 textboxes on the form called textbox1, textbox2 etc.
the following code in userform_initialize
---
Private Sub UserForm_Initialize()
Dim sTxt As String ' the text from a line
Open "c:\test\interview.txt" For Input As #1
Input #1, sTxt ' get text from line 1
TextBox1.Text = sTxt ' put it in TextBox1
Input #1, sTxt ' get text from line 2
TextBox2.Text = sTxt ' put it in TextBox2
Input #1, sTxt ' get text from line 3
TextBox3.Text = sTxt ' put it in TextBox3
Input #1, sTxt ' get text from line 4
TextBox4.Text = sTxt ' put it in TextBox4
Input #1, sTxt ' get text from line 5
TextBox5.Text = sTxt ' put it in TextBox5
Close #1
End Sub
---
the following code in a module in the dot.
---
Sub autonew()
FrmInterview.Show
End Sub
---
Then, everytime you create a new doc based on interview.dot
the userform shows and fills itself with the first 5 lines
from the textfile "c:\test\interview.txt".
And, get used to the terminology, otherwise people willing
to help may not understand you!
There are no formfields on a userform, in fact, no fields at all,
but controls. Here, these controls are textboxes. And a form
it something totally different from a userform.
HTH
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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