ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open - Output txt file creation (https://www.excelbanter.com/excel-programming/380614-open-output-txt-file-creation.html)

elcroco

Open - Output txt file creation
 
Hi everybody,

I would like to know whether I need to add a specific reference in my
vba project in order to be able to use the following kind of command:

dim lfnum as long

lfNum =FreeFile

Open strName &".txt" For Output as lFNum

Because by default whenI do that it is not recognized and the
compilation cannot be done

thans in advance for your answer

G


[email protected]

Open - Output txt file creation
 
There is nothing wrong with what you have written, but you have
magically created a variable called StrName in your code snippet and
(a) not declared it and (b) not set it - so there MAY be something in
the rest of your code that is actually the fault
elcroco wrote:

Hi everybody,

I would like to know whether I need to add a specific reference in my
vba project in order to be able to use the following kind of command:

dim lfnum as long

lfNum =FreeFile

Open strName &".txt" For Output as lFNum

Because by default whenI do that it is not recognized and the
compilation cannot be done

thans in advance for your answer

G



Chip Pearson

Open - Output txt file creation
 
Where, if ever, are you declaring and assigning a value to the variable
strName? If you do not have an Option Explicit declaration at the top of the
code module, and you haven't declared the strName variable, VBA will create
it automatically and give it a value of an empty string. Therefore, your
code is attempting to open a file named ".txt", and this will cause an
error. First of all, put Option Explicit at the very first line of your code
module (above and outside of any procedure). Then in the procedure, declare
the variable strName and give it a value:

Dim strName As String
strName = "C:\Test.txt" '<<<< CHANGE AS REQUIRED

Then call open:

Open strName &".txt" For Output as lFNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"elcroco" wrote in message
oups.com...
Hi everybody,

I would like to know whether I need to add a specific reference in my
vba project in order to be able to use the following kind of command:

dim lfnum as long

lfNum =FreeFile

Open strName &".txt" For Output as lFNum

Because by default whenI do that it is not recognized and the
compilation cannot be done

thans in advance for your answer

G




elcroco

Open - Output txt file creation
 
the variable is declared before, I ve just forgotten to write it in the
post !!!

wrote:
There is nothing wrong with what you have written, but you have
magically created a variable called StrName in your code snippet and
(a) not declared it and (b) not set it - so there MAY be something in
the rest of your code that is actually the fault
elcroco wrote:

Hi everybody,

I would like to know whether I need to add a specific reference in my
vba project in order to be able to use the following kind of command:

dim lfnum as long

lfNum =FreeFile

Open strName &".txt" For Output as lFNum

Because by default whenI do that it is not recognized and the
compilation cannot be done

thans in advance for your answer

G




All times are GMT +1. The time now is 07:28 AM.

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