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

Opening a text file with a macro?

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
Hello people!

Can someone tell me how I can open a text file, read a single small string of text and stick it into a cell with a macro within Excel? I'm told there's a function called "GetFileName()" or something like that. Can anyone tell me what the correct function is? For that matter, what function or statement I need to read the string of text?

Thanks,
Jisoo22
 
Reading and writing text from and to txt files is easy, the following code will do so:

Function Infile_ReadText() as String
Dim strFile as string
Dim strText as string
Dim strOutPut as string

strFile="C:\Temp\Textfilename.txt"

open strFile for Input as #1
While not EOf(1)
Input #1,strText
strOutPut=strOutPut & strText
Wend
Close #1
Infile_REadText=strOutPut
End function

Sub Paste_InCell
Dim strToPaste as string

strToPaste=Infile_ReadText
ActiveWorkbook.Worksheets("Sheet1").Cells("A1").Value=strToPaste

End sub

Writing to a textfile uses the same syntax, replace Open for 'Input' with 'OutPut' and instead of Input #1, use Write #1.

Have Fun!
CookieMonster


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top