是否有适用于 Mac 的 Excel 2011 VBA 中 Scripting.FileSystemObject 的替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4677881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:30:53  来源:igfitidea点击:

Is there an alternative to Scripting.FileSystemObject in Excel 2011 VBA for the mac?

macosexcelvbafilesystemobjectexcel-vba-mac

提问by Edward Tanguay

The answers to How can I install/use “Scripting.FileSystemObject” in Excel 2011 for MAC?seem to indicate that using Scripting.FileSystemObjectin Excel 2010 for the mac is notpossible.

如何在 Excel 2011 for MAC 中安装/使用“Scripting.FileSystemObject”的答案似乎表明无法Scripting.FileSystemObject在 Excel 2010 中为 mac使用。

What other alternativeis available so I can:

还有什么其他选择可用,以便我可以:

  • get a collectionof all Excel files in a specific directory
  • iteratethrough each worksheet within each file and export it to a .csvfile
  • 获取特定目录中所有 Excel 文件的集合
  • 遍历每个文件中的每个工作表并将其导出到.csv文件

Currently this is a six-stepprocess for each file:

目前,这是每个文件的六步过程:

--how to create CSV files for all worksheets in a file:
1. open file
2. click "Developer"
3. click editor
4. click ThisWorkbook
5. copy in:
Sub save_all_csv()
    On Error Resume Next
    Dim ExcelFileName As String
    ExcelFileName = ThisWorkbook.Name
    For Each objWorksheet In ThisWorkbook.Worksheets
        Filename = "FILE-" & ExcelFileName & "-WORKSHEET-" & objWorksheet.Name & ".csv"
        objWorksheet.SaveAs Filename:="Macintosh HD:Users:edward:Documents:temporaryNoBackup:" & Filename, FileFormat:=xlCSV, CreateBackup:=False
    Next
    Application.DisplayAlerts = False
    Application.Quit
End Sub
6. click run (it closes by itself)

I'm looking for a way to automatethis on the Mac, ideally, a (cron job?, service?) would open the excel file every 10 minutes, which would in turn look in a directory, convert all the other Excel files to .csv files, and then close by itself.

我正在寻找一种在 Mac 上自动执行此操作的方法,理想情况下,(cron 作业?,服务?)将每 10 分钟打开一次 excel 文件,这将依次查看目录,将所有其他 Excel 文件转换为.csv 文件,然后自行关闭。

Without Scripting.FileSystemObject, how can I make this Excel-to-CSV conversion fully automatic on the Mac?

如果没有 Scripting.FileSystemObject,我如何才能在 Mac 上完全自动进行 Excel 到 CSV 的转换?

回答by Fink

The only way I can think of is using the "Dir" function. Since mac supports extra characters in their filenames, wildcards do not work with the "Dir" function. Here is a sample.

我能想到的唯一方法是使用“Dir”函数。由于 mac 支持文件名中的额外字符,通配符不适用于“Dir”函数。这是一个示例。

Function GetFileList(folderPath As String) As Collection
'mac vba does not support wildcards in DIR function

    Dim file As String
    Dim returnCollection As New Collection

    If Right$(folderPath, 1) <> "/" Then
        folderPath = folderPath & "/"
    End If

    file = Dir$(folderPath) 'setup initial file

    Do While Len(file)
        returnCollection.Add folderPath & file
        file = Dir$
    Loop

    Set GetFileList = returnCollection
End Function