Hi,
The following VBA code runs perfectly on a Windows 2000 Pro. SP4 with Microsoft Office 2000 Pro (using Word) but failed when ran on a Windows XP Pro. SP2 with Microsoft Office 2000 Pro (using Word).
Const iNO_NEW_FOLDER_BUTTON As Integer = 512
Const iBROWSE_INCLUDE_FILES As Integer = 16384
Dim objShellApp ' Folder browser window.
Dim objFileName ' Selected file (by the user).
Set objShellApp = CreateObject("Shell.Application")
Set objFileName = objShellApp.BrowseForFolder(0, "", iNO_NEW_FOLDER_BUTTON + iBROWSE_INCLUDE_FILES, "") ' <- BUG LINE.
Dim strSelectedFilePathName As String
' If the OK button has been clicked:
If (Not objFileName Is Nothing) Then
' Get the selected file's path and name.
strSelectedFilePathName = objFileName.self.Path
' Else, the CANCEL button has been clicked:
' No action required.
End If
MsgBox strSelectedFilePathName
' Memory cleanup.
Set objFileName = Nothing
Set objShellApp = Nothing
When the BUG LINE executes, on a Windows XP Pro. SP2 with Microsoft Office 2000 Pro (using Word), and a file (not a folder) has been selected (from the BrowseForFolder function), I get the following error message:
Method 'BrowserForFolder' of object 'IShellDispatch4' failed
Then, if I select a folder (not a file) or modify the BUG LINE as the following (leaving no choice but to select only a folder):
Set objFileName = objShellApp.BrowseForFolder(0, "", iNO_NEW_FOLDER_BUTTON, "")
and run the code on a Windows XP Pro. SP2 with Microsoft Office 2000 Pro (using Word), no error message occurs and I get the selected folder's path.
So, I think the problem lies in the fact that when a file (not a folder) is selected (using the BrowserForFolder function) this function cannot handle the selected file.
Why? Is there something wrong with my code? Is it normal that the BrowseForFolder function cannot handle files?
Thanks.