Amending code for a file rather than a folder

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
Hi

I picked up some code from the board for saving and zipping a file - but it's actually for saving a folder. I only understand some of the code and would like to save a file rather than a folder. I changed some of the code to ask for a file(folder) location several times and then stop but it still wants to zip the file.

How do I amend the code to look for a file rather than a folder and also make the routine stop if no file is selected?

Code:
Function GetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
    bInfo.pidlRoot = 0& ' Root folder = Desktop
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a file."
        ' the dialog title
    Else
        bInfo.lpszTitle = Msg ' the dialog title
    End If
    bInfo.ulFlags = &H1 ' Type of directory to return
    X = SHBrowseForFolder(bInfo) ' display the dialog
    ' Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal X, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetFolderName = Left(path, pos - 1)
    Else
        GetFolderName = ""
    End If
End Function
Sub TestGetFolderName()
Dim oFSObj As Object
tryagain: 'forces folder selection..can't exit
    cnt = cnt + 1
    foldername = GetFolderName("Select a folder")
    If foldername = "" Then
        MsgBox "You didn't select a folder."
        If cnt < 3 Then 'change number of attempts
        GoTo tryagain
      Else
      Set fsoObj = Nothing
      cnt = 0
      MsgBox "A file was not selected."
End If
End If
End Sub

Regards
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hey Glaswegian

I don't have a copy of the BROWSEINFO class. Is it a Visual Basic, windows or a custom written class?

anvil19
:eek:
 
Upvote 0
Hi Anvil19

I got the code from the board and I'm not sure I understand all of it. I don't know where BROWSEINFO came from.

Sorry if that's not any help but much of the code is way too advanced for me.

Regards
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top