我在 Mac 上运行 Excel 2011,而我的一个适用于 Windows 版本的应用程序在我的 Mac 上不起作用。它在 Application.FileDialog 中找不到方法“FileDialog”。
我正在使用参考 ms office 14.0 对象库、vba、ms excel 14.0 对象库、ole 自动化和 ms forms 2.0 对象库。
为什么我的 Mac 上的应用程序类不存在 FilDialog 方法,但它在我的 Windows 上有效?
Application.FileDialog
根据本文,Excel 2011 中不存在。试试这个解决方案,它会创建一个函数。
解决方案如下,并允许它在 Macintosh 上运行:
Function myGetOpenFileName(Optional sPath As String) As String
Dim sFile As String
Dim sMacScript As String
If isMac Then
If sPath = vbNullString Then
sPath = "the path to documents folder"
Else
sPath = " alias """ & sPath & """"
End If
sMacScript = "set sFile to (choose file of type ({" & _
"""com.microsoft.Excel.xls"",
""org.openxmlformats.spreadsheetml.sheet"",
""public.comma-separated-values-text"", ""public.text"",
""public.csv"",
""org.openxmlformats.spreadsheetml.sheet.macroenabled""}) with prompt " & _
"""Select a file to import"" default location " & sPath & ") as string" _
& vbLf & _
"return sFile"
Debug.Print sMacScript
sFile = MacScript(sMacScript)
Else 'windows
sFile = Application.GetOpenFilename("CSV files,*.csv,Excel 2007 files,*.xlsx", 1, _
"Select file to import from", "&Import", False)
End If