rollsch
rollsch

Reputation: 2780

VBA write to text file: Run-time error 52 'bad file name or number'

Public Sub EdgeColor_Click()
    Dim intInFile As Integer
    intInFile = FreeFile

    Open "c:\picturename.csv" For Output As #intFileNo
    Print #intFileNo, "test"

    Close #intFileNo
End Sub

This is my code, and I get Run-time error 52 'bad file name or number'. I've tried doing as Input and making the file exist but I get the same error.

This is VBA inside of a Proficy iFix graphic, and can't for the life of me figure out what I am doing wrong.

Upvotes: 3

Views: 16126

Answers (1)

paxdiablo
paxdiablo

Reputation: 882446

You're mixing up intInFile and intFileNo.

It's possible that intFileNo is simply set to zero if you're not using the option explicit command, which may explain the invalid number.

That little command goes at the top of every one of my VB/VBA files because I've been burnt by this so many time before.

Upvotes: 10

Related Questions