ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   writing a text file in the same folder as my program (https://www.excelbanter.com/excel-programming/404638-writing-text-file-same-folder-my-program.html)

Takashi Yamauchi

writing a text file in the same folder as my program
 
Could someone helps me?

I'm trying to write data on "TestData.txt", but place "TestData.txt" in the
same folder as my Excel program is placed.


Private Sub SaveThisSubInforGlobal()
Sheets("Response_2").Select

Open "TestData.txt" For Append As #1


Write #1, Cells(i, 1).Value; Cells(i, 2).Value; Cells(i, 3).Value

Close #1
End Sub


I find "TestData.txt" sometimes in the same folder as my Excel program is
placed, but also sometimes in "My Document." It is not consistent. Why is
this so? Is there any way I can place "TestData.txt" in the same folder as
my program is placed? I could do this easily with VB.6, but Excel VBA is
not consistent....


Thank you



Helmut Weber

writing a text file in the same folder as my program
 
Hi Takashi Yamauchi,

MsgBox Application.Path

but I don't consider it a good idea
to place data in the same location as the program's exe-file.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

[email protected]

writing a text file in the same folder as my program
 


Takashi Yamauchi wrote:
Is there any way I can place "TestData.txt" in the same folder as
my program is placed?


I don't know what is setting or varying the working directory in your
case. But here are some ideas so you can set it yourself right before
the write:

1. Assuming you want to set it to the directory of your .xls file you
can use ChDir Activeworkbook.path

2. If you really want to set it to the directory of the Excel
executable (excel.exe) you can use ChDir Application.path

equiangular

writing a text file in the same folder as my program
 
If activeworkbook has not been saved, the curdir will be MyDocuments

Takashi Yamauchi wrote:
Could someone helps me?

I'm trying to write data on "TestData.txt", but place "TestData.txt" in the
same folder as my Excel program is placed.


Private Sub SaveThisSubInforGlobal()
Sheets("Response_2").Select

Open "TestData.txt" For Append As #1


Write #1, Cells(i, 1).Value; Cells(i, 2).Value; Cells(i, 3).Value

Close #1
End Sub


I find "TestData.txt" sometimes in the same folder as my Excel program is
placed, but also sometimes in "My Document." It is not consistent. Why is
this so? Is there any way I can place "TestData.txt" in the same folder as
my program is placed? I could do this easily with VB.6, but Excel VBA is
not consistent....


Thank you



Takashi Yamauchi

writing a text file in the same folder as my program
 

wrote in message
...


Takashi Yamauchi wrote:
Is there any way I can place "TestData.txt" in the same folder as
my program is placed?


I don't know what is setting or varying the working directory in your
case. But here are some ideas so you can set it yourself right before
the write:

1. Assuming you want to set it to the directory of your .xls file you
can use ChDir Activeworkbook.path


Thank you. This worked, but only when I'm running the program from my main
hard disk. When I was running the program from a USB stick, the TestData.txt
is still placed in "My Document"....

But this is good enough.

Thank you again

takashi yamauchi


2. If you really want to set it to the directory of the Excel
executable (excel.exe) you can use ChDir Application.path





All times are GMT +1. The time now is 10:16 AM.

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