Iulian
Iulian

Reputation: 319

Can't open file in Excel VBA, Run-time error '52'

So I've got this code

Dim fpath As String
fpath = "D:\stats.csv"
Open fpath For Append As #fw

However when I am trying to run it I am getting a run-time error '52': Bad file name or number.

I have no idea what I am doing wrong, maybe it's a Office 2016/Windows 10 glitch that I am not aware of.

Thanks in advance for your help

Upvotes: 2

Views: 1069

Answers (1)

A.S.H
A.S.H

Reputation: 29332

You need fw to be a valid file identifier number. To do so, get an identifier from the function FreeFile:

Dim fpath As String, fw As Long
fw = FreeFile                   ' <--------- get a valid file identifier
fpath = "D:\stats.csv"
Open fpath For Append As #fw

Upvotes: 2

Related Questions