Please would someone know how to enter text from a text file (the whole
file contents) into Word. I have seen some code on the forums, but
don't understand how it works.

Found this simple code, but it will only input the last set of
characters in the file.


Dim MyString
Open "C:\files\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, MyString
Loop
Close #1


ActiveDocument.Bookmarks("Test").Range.Text = MyString


Thanks in advance,
Net

Re: Inserting text from a text file into Word by Tony

Tony
Fri Nov 03 11:48:45 CST 2006

What your code is doing is reading the file line by line into the variable
'MyString', each line replacing the previous one. At the end you write the
contents of 'MyString', then containing the last line as you have seen, to
your bookmark.

You could shift the moving text to your document inside the loop and
continue line by line but you would need to alter it slightly to work
properly with the bookmark, or ...

... you could do the whole thing more easily with ...

ActiveDocument.Bookmarks("Test").Range.InsertFile
FileName:="C:\files\test.txt", Link:=False

--
Enjoy,
Tony

<aef1@lycos.co.uk> wrote in message
news:1162401653.953319.173010@f16g2000cwb.googlegroups.com...
> Please would someone know how to enter text from a text file (the whole
> file contents) into Word. I have seen some code on the forums, but
> don't understand how it works.
>
> Found this simple code, but it will only input the last set of
> characters in the file.
>
>
> Dim MyString
> Open "C:\files\test.txt" For Input As #1
> Do While Not EOF(1)
> Input #1, MyString
> Loop
> Close #1
>
>
> ActiveDocument.Bookmarks("Test").Range.Text = MyString
>
>
> Thanks in advance,
> Net
>


Re: Inserting text from a text file into Word by Jay

Jay
Wed Nov 01 11:58:09 CST 2006

If you want to do it this way, you have to collect the lines as they're read
from the file and append them to a string:

Dim tempString, MyString
Open "C:\files\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, tempString
MyString = MyString & tempString & vbCr
Loop
Close #1
' remove last vbCr
MyString = Left(MyString, Len(MyString) - 1)

ActiveDocument.Bookmarks("Test").Range.Text = MyString

But you can do the whole thing in one step with the InsertFile command:

ActiveDocument.Bookmarks("Test").Range.InsertFile _
FileName:="c:\files\test.txt"

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

aef1@lycos.co.uk wrote:
> Please would someone know how to enter text from a text file (the
> whole file contents) into Word. I have seen some code on the forums,
> but don't understand how it works.
>
> Found this simple code, but it will only input the last set of
> characters in the file.
>
>
> Dim MyString
> Open "C:\files\test.txt" For Input As #1
> Do While Not EOF(1)
> Input #1, MyString
> Loop
> Close #1
>
>
> ActiveDocument.Bookmarks("Test").Range.Text = MyString
>
>
> Thanks in advance,
> Net



Re: Inserting text from a text file into Word by Tony

Tony
Wed Nov 01 12:55:04 CST 2006

Oops - seems like I was (or will be) posting from the future.

I have corrected it but am using Windows Mail on Vista Beta 2 so it may go
wrong again.

--
Enjoy,
Tony

"Tony Jollans" <My forename at my surname dot com> wrote in message
news:%23xX154d$GHA.4496@TK2MSFTNGP02.phx.gbl...
> What your code is doing is reading the file line by line into the variable
> 'MyString', each line replacing the previous one. At the end you write the
> contents of 'MyString', then containing the last line as you have seen, to
> your bookmark.
>
> You could shift the moving text to your document inside the loop and
> continue line by line but you would need to alter it slightly to work
> properly with the bookmark, or ...
>
> ... you could do the whole thing more easily with ...
>
> ActiveDocument.Bookmarks("Test").Range.InsertFile
> FileName:="C:\files\test.txt", Link:=False
>
> --
> Enjoy,
> Tony
>
> <aef1@lycos.co.uk> wrote in message
> news:1162401653.953319.173010@f16g2000cwb.googlegroups.com...
>> Please would someone know how to enter text from a text file (the whole
>> file contents) into Word. I have seen some code on the forums, but
>> don't understand how it works.
>>
>> Found this simple code, but it will only input the last set of
>> characters in the file.
>>
>>
>> Dim MyString
>> Open "C:\files\test.txt" For Input As #1
>> Do While Not EOF(1)
>> Input #1, MyString
>> Loop
>> Close #1
>>
>>
>> ActiveDocument.Bookmarks("Test").Range.Text = MyString
>>
>>
>> Thanks in advance,
>> Net
>>
>


Re: Inserting text from a text file into Word by aef1

aef1
Thu Nov 02 03:39:38 CST 2006

Thank you so much Jay!

The one step is great. I tried it and it worked fine for what I wanted
to do.