How do I put text in a TextBox

J

jkligel

I'm very new at this and would like some help. I'm using Word 2003, VB
6.3. I'm trying to learn how to get text from a file (.txt or.doc) on
the C-Drive and put it into a TextBox. Can anyone help?

If this is basic, keep in mind I'm at the very beginning and slowly
learning vba.

Thanks
Joe
 
J

Jezebel

What have you tried so far? Help explains how to read text from a file on
disk (methods are *completely* different for Docs and for text files). Get
this part working for a start.

Then read Help on StoryRanges and ShapeRanges.
 
J

jkligel

John said:
As Jezebel says, getting text from text files and Word documents are
totally different processes.

If it's a text file, there's a readymade procedure here
http://www.j.nurick.dial.pipex.com/Code/VBA/FileContents.htm which reads
the contents of a text file into a VBA variable.


Thanks for the link. I tried and tried to use it but I couldn't get it
to work,However........
I did manageto get a MsgBox to appear...which I think I'm close
(Wishful thinking,lol)
Here is the code I used;

Private Sub CopyFile_Click()
ChangeFileOpenDirectory "C:\"
Dim TextLine
Open "MYTEST1.txt" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Debug.Print TextLine ' Print to the Immediate window.
MsgBox (TextLine)
Loop
Close #1 ' Close file.

End Sub

If I could only get it into the actual TextBox ("TextBox1"),
Do you have any more sugestions?

Joe
 
J

John Nurick

The FileContents() function works fine for me. Here's a demo sub that
uses it to put the contents of a text file into the TextFrame of a Shape
such as a textbox:

Public Sub LoadTextBoxFromFile(S As Shape, FileSpec As String)
Dim NewText As Variant
Dim ErrCode As Long

NewText = FileContents(FileSpec, False, ErrCode)
If IsNull(NewText) Then
MsgBox "FileContents function reported error " _
& ErrCode & ": " & Error(ErrCode) & vbCrLf _
& "reading " & FileSpec, vbExclamation + vbOKOnly
Else
S.TextFrame.TextRange.Text = NewText
End If
End Sub

It's a long time since I did much with Word textboxes, but I think you'd
call it with something like this to specify the textbox (watch out for
wrapping)

LoadTextBoxFromFile _
ActiveDocument.StoryRanges(wdMainTextStory).ShapeRange("Text Box 1"),
_
"D:\Folder\File.txt"
 
J

Jezebel

Private Sub CopyFile_Click()

Dim Text as string
Dim pFileNum as long

'Open and read the file
pFileNum = FREEFILE
Open "C:\MYTEST1.txt" For Input As #pFileNum
pText = input(LOF(pFileNum),pFileNum)
Close #pFileNum

'Show that it worked
Msgbox pText

'Assign the text to a range in the document
ActiveDocument.Bookmarks("FileText").Range = pText

End Sub
 
J

jkligel

Thanks Jezebel,

That worked, still having problems getting it into the text box.
It's named "TextBox1"

I run it and it works up until you click the ok button (after the
message) then i get a '5941' run-time error...stating "The requested
member of the collection does not exist"

Here are all the subs;

Private Sub CLEARTEXTbox_Click()
TextBox1.Text = ""
End Sub

Private Sub ClearWORD_Click()
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

Private Sub CopyFile_Click()

Dim Text As String
Dim pFileNum As Long


'Open and read the file
pFileNum = FreeFile
Open "C:\MYTEST1.txt" For Input As #pFileNum
pText = Input(LOF(pFileNum), pFileNum)
Close #pFileNum


'Show that it worked
MsgBox pText


'Assign the text to a range in the document
ActiveDocument.Bookmarks("FileText").Range = pText


End Sub




Private Sub PrintTextBox_Click()
Selection.TypeText [TextBox1]
End Sub

Private Sub PrintToWord_Click()
jck = "This is a test of print to word"
For x = 1 To 5
Selection.TypeText jck
Selection.TypeParagraph
Next x

End Sub

Private Sub TextBox1_Change()

End Sub

I don't know if this helps, also keep in mind.. very simple stuff,
cause I'm slowly picking up VBA.

Thanks,
Joe
 
R

Russ

jkligel,
See within text.
Thanks Jezebel,

That worked, still having problems getting it into the text box.
It's named "TextBox1"

I run it and it works up until you click the ok button (after the

What sub gets run when you click the ok button and which vba code line is
highlighted when the error message is invoked?
message) then i get a '5941' run-time error...stating "The requested
member of the collection does not exist"

The only collection I see is the 'x' collection.
Here are all the subs;

Private Sub CLEARTEXTbox_Click()
TextBox1.Text = ""
End Sub

Private Sub ClearWORD_Click()
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

Private Sub CopyFile_Click()

Dim Text As String
Dim pFileNum As Long


'Open and read the file
pFileNum = FreeFile
Open "C:\MYTEST1.txt" For Input As #pFileNum
pText = Input(LOF(pFileNum), pFileNum)
Close #pFileNum


'Show that it worked
MsgBox pText

Did you want?:
Textbox1.Text = pText
'Assign the text to a range in the document
ActiveDocument.Bookmarks("FileText").Range = pText

Or did you want?:
ActiveDocument.Bookmarks("Textbox1").Range = pText
End Sub




Private Sub PrintTextBox_Click()
Selection.TypeText [TextBox1]

The line above seems strange to me. Is it the collection error line? Do you
want?:
Selection.TypeText "[TextBox1]"
Or?
Selection.TypeText TextBox1.Text
The .TypeText method expects to see something that evaluates to a string of
characters.
End Sub

Private Sub PrintToWord_Click()
jck = "This is a test of print to word"
For x = 1 To 5
Selection.TypeText jck
Selection.TypeParagraph
Next x

Do you have Option Explicit declared in the declaration section of your code
project to help show when variables are undeclared? For instance the 'x'
variable doesn't seem to be declared.
End Sub

Private Sub TextBox1_Change()

End Sub

I don't know if this helps, also keep in mind.. very simple stuff,
cause I'm slowly picking up VBA.

Thanks,
Joe


Private Sub CopyFile_Click()

Dim Text as string
Dim pFileNum as long

'Open and read the file
pFileNum = FREEFILE
Open "C:\MYTEST1.txt" For Input As #pFileNum
pText = input(LOF(pFileNum),pFileNum)
Close #pFileNum

'Show that it worked
Msgbox pText

'Assign the text to a range in the document
ActiveDocument.Bookmarks("FileText").Range = pText

End Sub



John Nurick wrote:
As Jezebel says, getting text from text files and Word documents are
totally different processes.

If it's a text file, there's a readymade procedure here
http://www.j.nurick.dial.pipex.com/Code/VBA/FileContents.htm which reads
the contents of a text file into a VBA variable.

On 23 Aug 2006 12:19:05 -0700, (e-mail address removed) wrote:

I'm very new at this and would like some help. I'm using Word 2003, VB
6.3. I'm trying to learn how to get text from a file (.txt or.doc) on
the C-Drive and put it into a TextBox. Can anyone help?

If this is basic, keep in mind I'm at the very beginning and slowly
learning vba.

Thanks
Joe

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.


Thanks for the link. I tried and tried to use it but I couldn't get it
to work,However........
I did manageto get a MsgBox to appear...which I think I'm close
(Wishful thinking,lol)
Here is the code I used;

Private Sub CopyFile_Click()
ChangeFileOpenDirectory "C:\"
Dim TextLine
Open "MYTEST1.txt" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Debug.Print TextLine ' Print to the Immediate window.
MsgBox (TextLine)
Loop
Close #1 ' Close file.

End Sub

If I could only get it into the actual TextBox ("TextBox1"),
Do you have any more sugestions?

Joe
 

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