Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Problem with the BrowseForFolder function

$
0
0
<!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; mso-bidi-font-size:12.0pt; font-family:Arial; mso-fareast-font-family:"Times New Roman"; mso-bidi-font-family:"Times New Roman";} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

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.

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>