Specify the path to the user folder in VBA for Windows and Mac

I am writing a tool that generates a file from Excel using VBA. The generated file is written to the folder, if it exists, in the "User Documents" folder.

eg. C:\Users\<username>\Documents\Some Folder\

If the last folder does not exist, then VBA creates it. I use the following line to make sure the folder location works for different Windows users distributed in the organization:

If Len(Dir(Environ("USERPROFILE") & "\Documents\Some Folder", vbDirectory)) = 0 Then
    MkDir Environ("USERPROFILE") & "\Documents\Some Folder"
End If
Open Environ("USERPROFILE") & "\Documents\Some Folder\" & "file.php" For Output As #1
Print #1, output
Close

Now my problem is that I also have to serve Mac OSX users. I currently do not have access to a Mac for testing, but I assume that the above will not work.

What can I use to specify subfolders in user documents and how to include code that conditionally uses the Windows line or the Mac line?

+4
2

, . . .

Mac Windows :

#if Mac then
'here Mac only code
#else
'here Windows only code
#end if

Mac, , MacScript, , script, .

+7

:

1. Application.PathSeparator... MSDN.

:

Dim PS as String
PS = Application.PathSeparator
If Len(Dir(Environ("USERPROFILE") & PS & "Documents" & PS & "Some Folder", vbDirectory)) = 0 Then
    MkDir Environ("USERPROFILE") & PS & "Documents" & PS & "Some Folder"
End If
Open Environ("USERPROFILE") & PS & "Documents" & PS & "Some Folder" & PS & "file.php" For Output As #1
Print #1, output
Close

2. IF THEN OS

:

Sub SaveDoc()
    Dim wksSheet As Worksheet

    If InStr(1, Application.OperatingSystem, "Windows") > 0 Then
      'Code with Windows Path Separator
      Exit Sub

    Else
      'Code with Mac Path Separator
      Exit Sub
    End If

End Sub
+6

Source: https://habr.com/ru/post/1658202/


All Articles