当我通过 Excel 运行这段代码时,它说 Left(,) 函数需要一个数组。我传递给它 a String
,函数需要 a String
,变量被声明为 a String
。我把$
操作员放在那里,它仍然让我很废话。知道会发生什么吗?
FWIW 当我通过 SolidWorks 运行它时,它执行得很好。
此外,它是从传递给它的用户窗体调用的String
。
#If VBA7 Then
Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
pszPath As String) As Long
Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long
#Else
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long
#End If
Public Type BROWSEINFO
#If VBA7 Then
hOwner As LongPtr
pidlRoot As LongPtr
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As LongPtr
lParam As LongPtr
iImage As Long
#Else
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
#End If
End Type
Private bInfo As BROWSEINFO
Function GetDirectory(Optional Msg As String = "Select a folder.") As String
Dim path As String
Dim x As Long, pos As Integer
'dim
bInfo.pidlRoot = 0& ' Root folder = Desktop
bInfo.lpszTitle = Msg ' Dialog title
bInfo.ulFlags = &H1 ' Type of directory to return
x = SHBrowseForFolder(bInfo)
path = Space$(512)
If SHGetPathFromIDList(ByVal x, ByVal path) Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, (pos - 1))
End If
End Function