send contents of cell b4 to a plain text file

  • Thread starter Thread starter lothario
  • Start date Start date
L

lothario

How do I send the data in cell b4 to a plain text file
c:\data\description.txt

Cell b4 contains a function and the result is some lengthy text data

If the c:\data\description.txt file exists then it should be
overwritten.
 
It just takes a few lines of VB code:

Sub export_text()
textstring = Range("B4").Value
Open "c:\data\description.txt" For Output As #1
Print #1, textstring
Close #1
End Sub

HTH,
Nikos
 
A follow up question - assume that the hostname on my PC is sdesk1 then
how can
Open "c:\data\description.txt" For Output As #1
be changed so that the hostname is part of the filename so now it opens
the file
"c:\data\sdesk1_description.txt"
insted of just
"c:\data\description.txt"


This description file is on network so I need to make sure that it is
not overwritten
by someone else on the network using the same script.
 
Good point! You could easily use your computer name (is this what you are
referring to by host name?) or your Windows user name (or even both),
assuming that no two PCs and no two users on the network will have the same
name. It's good practice, as far as I am concerned. To get either through
VB, you would use Environ("ComputerName") and Environ("UserName")
respectively. So your code could be:

unam = Environ("UserName")

fnam = "C:\data\" & unam & "_description.txt"

Open fnam for Output as #1



Or modify for computer name.



HTH,

Nikos
 
Back
Top