2017-04-27 262 views
0

我有一個功能可以在Mac(Excel 2015)上打開文件選取器對話框並返回路徑。代碼如下,但我認爲路徑是問題,而不是代碼,我沒有Mac來自己調試它。該函數返回的路徑是:Excel VBA - 在Mac上找不到文件

SSD:用戶:用戶名:下載:時間表出口(40).csv格式

我修改爲:用戶名:但它正確地顯示用戶。該文件確實在此位置上,我要求用戶發送我的路徑和他們派:

/用戶/用戶名/下載/時間表出口(40).csv格式

Function GetFileMac() 

Dim myPath As String 
Dim myScript As String 
Dim myFiles As String 
Dim mySplit As Variant 
Dim n As Long 
Dim fName As String 
Dim myBook As Workbook 

On Error Resume Next 
myPath = MacScript("return (path to documents folder) as String") 

myScript = _ 
"set applescript's text item delimiters to "","" " & vbNewLine & _ 
      "set theFiles to (choose file of type " & _ 
     " {""com.microsoft.excel.xls"",""public.comma-separated-values-text""} " & _ 
      "with prompt ""Please select the time sheet file"" default location alias """ & _ 
      myPath & """ multiple selections allowed false) as string" & vbNewLine & _ 
      "set applescript's text item delimiters to """" " & vbNewLine & _ 
      "return theFiles" 

myFiles = MacScript(myScript) 
On Error GoTo 0 

If myFiles <> "" Then 

    mySplit = Split(myFiles, ",") 
    GetFileMac = mySplit(0) 

End If 

End Function 

當我跑,我得到錯誤1004文件中沒有發現,但上面的路徑所提到的回升,是在FP串可用:

Dim fp as string 

' if user on Mac etc... then 

fp = GetFileMac 

Set someWorkbook = Application.Workbooks.Open(fp) ' error here 

回答

1

您可能需要更改路徑格式。

爲了使這樣的

SSD:Users:UserName:Downloads:timesheet-export (40).csv 

一個MAC路徑爲POSIX路徑這樣

/Users/UserName/Downloads/timesheet-export (40).csv 

用途:

set myPath to POSIX path of myPath