在VBA for Mac中写入具有长名称的文件

时间:2014-05-10 23:33:07

标签: vba excel-vba applescript excel-vba-mac applescript-excel

我有一个子程序,它将文本写入VBA中的.txt文件:

Sub WriteToFile(fileName as String, content as String)
  Open fileName For Output As #1
  Print #1, content
  Close #1
End Sub

此子例程在Windows下运行良好。但是,我意识到在Mac下,对于任何超过29个字符的targeFile,行Bad file name or number上会出现错误Open fileName For Output As #1

但我确实需要写很多名字很长的文件......

实际上,这个问题很常见。例如,this link也提到了There are problems with long file names when you use Dir on a Mac, 27/28 characters (without the ext) is the maximum(with the extension this is 32 characters).

似乎AppleScript可能是解决此问题的方法。但是,当我正在寻找编写而不是Dir的解决方法时,我找不到编写的精确解决方案。

有谁知道如何实现这个目标?

3 个答案:

答案 0 :(得分:0)

这是早期删除答案的更强大版本。与使用do shell script的已删除答案不同,此答案仅使用AppleScript命令字符串中的AppleScript功能,并且还正确地转义嵌入命令字符串中的参数。

尝试以下VBA过程,该过程合成AppleScript命令字符串以写入文件并使用MacScript执行。

' - Specify the output filename either as a mere filename -
'    in which case the file is created in the active workbook's folder -
'    or as a HFS path (with ':' as the path separator).
' - Note that line breaks in `content` are written as defined,
'    and that on a Mac `vbNewLine` is chr(13) (CR == \r) only.
'    If you want Unix-style LF (\n) line breaks instead, use chr(10) explicitly.
' Example:
'      WriteToFile "test.txt", "Hello, world." & Chr(10)
Sub WriteToFile(fileName As String, content As String)
    Dim filePath, filePathEscaped, contentEscaped, asCmd
    ' If the filename contains no path component, default to the active workbook's path.
    If InStr(fileName, ":") = 0 Then
        filePath = ActiveWorkbook.Path & ":" & fileName
    Else
        filePath = fileName
    End If
    ' In case the file path or input string contain double quotes, we must
    ' \-escape them for inclusing in the AppleScript command string first.
    filePathEscaped = Replace(filePath, """", "\""")
    contentEscaped = Replace(content, """", "\""")
    ' Synthesize the AppleScript command string.
    asCmd = _
       "set fRef to open for access """ & filePathEscaped & _
                                    """ with write permission" & Chr(13) & _
       "set eof fRef to 0" & Chr(13) & _
       "write """ & contentEscaped & """ to fRef" & Chr(13) & _
       "close access fRef"
    ' Execute the AppleScript command string.
    ' Any error will generically be reported as "Invalid procedure call or 
    ' argument", unfortunately.
    MacScript asCmd
End Sub

答案 1 :(得分:0)

是一种不优雅且不令人满意(但又容易)的替代方案
  1. 将输出写入保留的文件名,该文件名足够短,不会导致问题
  2. 写完成并关闭文件后,使用FileCopy将该命名短的文件复制到您想要的长名称
  3. 删除短命名文件
  4. 事实证明FileCopy对文件名长度并不那么挑剔。

    您可能希望将所有内容包含在检查中,以确保在编写之前保留名称不存在,并确保在保留名称的情况下删除具有保留名称的文件打开它。或者您可以向用户公开您正在做的事情,并在一开始就向用户查询这两个名称。 -HTH

答案 2 :(得分:0)

我知道这是一个旧线程但是......

作为解决方法,使用短名称创建文件,然后使用VBA语句Name将文件重命名为更长的名称。这适用于Excel Mac 2011。

  Open "Macintosh HD:Users:joe:Documents:SO-23587418.txt" For Output As #1
  Print #1, "Hello world!"
  Close #1

  Name "Macintosh HD:Users:joe:Documents:SO-23587418.txt" As "Macintosh HD:Users:joe:Documents:SO-23587418-11111111112222222222333333333344444444445555555555.txt"