I have the following function which produces a runtime error 52 (bad filename or number) only on Mac on the line Open filePath & filename For Output As #1
Function ExportAsCSV(ByVal charToEncode As String, _
ByVal filePath As String, ByVal filename As String) As Boolean
'Function to Export string to CSV. Returns True/False if successful
'Tested and working on PC XL2003,2007 & 2010 // MAC XL2011
'On Error GoTo Err:
On Error GoTo 0
Open filePath & filename For Output As #1
Print #1, charToEncode
Close #1
ExportAsCSV = True
On Error GoTo 0
Exit Function
Err:
On Error GoTo 0
ExportAsCSV = False
End Function
Example argument that causes an error is:
filePath : "Untitled:Users:ooo:Desktop:New" : String
filename : ":FloridaRhum Barbancourt 5 Star Reserve Speciál 8 Years Old.csv" : String
It’s definitely the filename causing an issue as if I change it to “:test.csv” as an example it works. All the individual words also work fine so it doesn’t seem to be an encoding issue and if I generate a long filename the same length it’s fine as well.
I’m stumped.. any ideas?
EDIT:
Also just to add, the Open filePath & filename For Output As #1 does actually generate a file with the correct filename in the correct location. But because the VBA generates an error nothing is written so the file is zero bytes.
I think you’ve hit a carry over bug which limits the save through VBA to 32 characters. You will still be able to create the file but won’t be able to open and write to it.
I’d either write to a shorter filename while processing and as the last step either use the
SaveAsmethod to save to a longer CSV filename or use AppleScript to rename the file. (I’m sure they’ll fix it in the next version of Office 😉