在VBA Excel 2003中获取"我的文档"文件夹的语言无关方式是使用Windows API函数SHGetSpecialFolderPath来获取文件夹路径。以下是一个示例代码:
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function SHGetSpecialFolderPath Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" (ByVal hwndOwner As LongPtr, ByVal lpszPath As String, ByVal nFolder As Long, ByVal fCreate As Boolean) As Boolean
#Else
Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, ByVal lpszPath As String, ByVal nFolder As Long, ByVal fCreate As Boolean) As Boolean
#End If
Const CSIDL_MYDOCUMENTS = &H5
Function GetMyDocumentsPath() As String
Dim strPath As String
Dim fResult As Boolean
strPath = Space$(260)
fResult = SHGetSpecialFolderPath(0, strPath, CSIDL_MYDOCUMENTS, True)
If fResult Then
GetMyDocumentsPath = Left$(strPath, InStr(strPath, Chr$(0)) - 1)
Else
GetMyDocumentsPath = ""
End If
End Function
这个代码示例定义了一个名为GetMyDocumentsPath的函数,该函数使用Windows API函数SHGetSpecialFolderPath来获取"我的文档"文件夹的路径。函数返回一个字符串,其中包含文件夹的路径。
请注意,这个代码示例仅适用于VBA Excel 2003。在更高版本的Excel中,您可能需要使用不同的方法来获取"我的文档"文件夹的路径。
领取专属 10元无门槛券
手把手带您无忧上云