Can anyone help me in passing handle values to a function as a variable.
This is to get the paths of all the special folders.
The undernoted code works perfectly well:
However, when I convert it to loop through entries I have in the worksheet, I get a type mismatch runtime 13 in the line containing the handle variable (hnd). I've tried various conversions such as CLng() as it is picking up entries in the column like &H1& &H2& ... &H5&. Is there a special way of passing a variable here.
All help greatly appreciated. Thanks.
This is to get the paths of all the special folders.
The undernoted code works perfectly well:
Code:
Private Declare Function GetFolderPath Lib "shfolder.dll" _
Alias "SHGetFolderPathA" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
ByVal hToken As Long, _
ByVal dwReserved As Long, _
ByVal lpszPath As String) As Long
Sub GetFldPath()
Dim sBuffer As String
sBuffer = Space$(260)
For Each sel in Selection
If GetFolderPath(&H5&, &H5&, -1, SHGFP_TYPE_default, sBuffer) = 0 Then
sel.Offset(0, 3).Value = Left$(sBuffer, StrPtr(sBuffer))
End If
Next
End Sub
However, when I convert it to loop through entries I have in the worksheet, I get a type mismatch runtime 13 in the line containing the handle variable (hnd). I've tried various conversions such as CLng() as it is picking up entries in the column like &H1& &H2& ... &H5&. Is there a special way of passing a variable here.
Code:
Sub GetFldPath()
Dim sBuffer As String
'Dim hnd As Long ' this line doesn't appear to help either way
sBuffer = Space$(260)
For Each sel In Selection
hnd = sel.Value
'MsgBox hnd ' this display all entries ok when enacted
'// To get the Path of each entry in the selection
If GetFolderPath(hnd, hnd, -1, SHGFP_TYPE_default, sBuffer) = 0 Then 'type mismatch here
sel.Offset(0, 3).Value = Left$(sBuffer, StrPtr(sBuffer))
End If
Next
End Sub
All help greatly appreciated. Thanks.