Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vba

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.

like image 448
rollsch Avatar asked Dec 30 '25 02:12

rollsch


1 Answers

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.

like image 110
paxdiablo Avatar answered Dec 31 '25 18:12

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!