Excel VBA代码 - 从网络文件夹打开文件 - PC与Mac

时间:2015-03-18 22:34:19

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

我正在尝试调整代码以在PC和Mac上工作,并且遇到访问网络文件夹上的文件的问题。以下是目前适用于PC用户的代码。我知道文件路径的语法对于Mac来说是不同的,但我似乎无法做到正确。我可以请求帮助我需要做出哪些改变吗?

Dim filename1 As String
Dim Path1 As String
Dim Name1 As String
Path1 = "\\Spr1rfpfil\groupdata\admin\sales\Database Share\Data Files\Misc Archive\"
Name1 = "Q4Data.xlsx"
filename1 = Path1 & Name1
Workbooks.Open fileName:=filename1

由于这是我的第一篇帖子,请告诉我以后是否应该采取不同的做法。

2 个答案:

答案 0 :(得分:1)

您可以使用“Application.OperatingSystem”

之类的内容

这将显示您的环境,然后相应地测试和设置路径

Dim filename1 As String: Dim Path1 As String: Dim Name1 As String

if instr(1, Application.OperatingSystem, "Mac", vbtextcompare) > 0 then
    Path1 = "/Volumes/Spr1rfpfil/groupdata/admin/sales/Database\ Share/Data\ Files/Misc\ Archive/" ' Replace with Mac path
Else
    Path1 = "\\Spr1rfpfil\groupdata\admin\sales\Database Share\Data Files\Misc Archive\" ' Replace with Windows path
End if

Name1 = "Q4Data.xlsx"
filename1 = Path1 & Name1
Workbooks.Open fileName:=filename1

答案 1 :(得分:0)

VBA有一个条件编译器常量,用于检测托管OS,您可以像这样使用它:

Dim filename1 As String: Dim Path1 As String: Dim Name1 As String

#If Mac Then
    Path1 = "/Volumes/Spr1rfpfil/groupdata/admin/sales/Database\ Share/Data\ Files/Misc\ Archive/" ' Replace with Mac path
#Else
    Path1 = "\\Spr1rfpfil\groupdata\admin\sales\Database Share\Data Files\Misc Archive\" ' Replace with Windows path
#End if

Name1 = "Q4Data.xlsx"
filename1 = Path1 & Name1
Workbooks.Open fileName:=filename1