ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   send contents of cell b4 to a plain text file (https://www.excelbanter.com/excel-programming/288606-send-contents-cell-b4-plain-text-file.html)

lothario[_68_]

send contents of cell b4 to a plain text file
 
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.


---
Message posted from http://www.ExcelForum.com/


Nikos Yannacopoulos[_5_]

send contents of cell b4 to a plain text file
 
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

"lothario " wrote in message
...
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.


---
Message posted from http://www.ExcelForum.com/




lothario[_69_]

send contents of cell b4 to a plain text file
 
Thank you Nikos


---
Message posted from http://www.ExcelForum.com/


lothario[_70_]

send contents of cell b4 to a plain text file
 
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.


---
Message posted from http://www.ExcelForum.com/


Nikos Yannacopoulos[_5_]

send contents of cell b4 to a plain text file
 
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

"lothario " wrote in message
...
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.


---
Message posted from http://www.ExcelForum.com/




lothario[_71_]

send contents of cell b4 to a plain text file
 
Got it. Thanks.


---
Message posted from http://www.ExcelForum.com/



All times are GMT +1. The time now is 12:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com