PDA

View Full Version : Save function on a userform



samuelimtech
04-14-2014, 03:06 AM
Morning all, [Assuming its morning where you are :) ]

Ive created a 'Tool' in word which is essentially a group of userforms and macros.
one function i am unable to work out is how to perform a save in a directory of the users choosing.
one way to achieve this is a text box and the user types in a path but thats not very user friendly so what im hoping for is (i think) is a combo box with a list of drives, then another combo box with the folders/subfolders found on that drive the result of which will result in a path for a save function to be performed.

hope ive been clear, thanks in advance

Bob Phillips
04-14-2014, 03:22 AM
Use


Application.FileDialog(msoFileDialogSaveAs)

it will thorw out a file browser dialog. You can pre-load the name, the user can determine where it goes. Be aware, it doesn't save it, just returns the selected path, you will still need to save it.

samuelimtech
04-14-2014, 03:34 AM
that isnt a problem, thank you very much. im assuming that I just plant it in the combobox code page?

Bob Phillips
04-14-2014, 04:41 AM
Wherever you want to do the save from.

macropod
04-14-2014, 06:32 PM
Another approach would be to use a folder browser:

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
This one even allows the user to create the save folder, if need be. To use the function, you might call it from a sub with code like:

Dim strFolder As String
strFolder = GetFolder
If strFolder = "" Then Exit Sub
'A folder was selected, so continue

fumei
04-14-2014, 09:23 PM
As a variation:
Sub Test()
MsgBox GetFolderWithPointer("C:\Program Files")
End Sub

Function GetFolderWithPointer(Optional Pointer)
Dim oFolder As Object
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Demo", 0, Pointer)
GetFolderWithPointer = oFolder.self.Path
End Function This variation is useful when you may want to restrict things a little or to give you the ability to define the starting folder. The restriction (if desired) comes from the starting folder. In the example Sub Test(), the starting folder is C:\Program Files, and you can not go above that folder. In other words you can not choose C:, or any other folder off the root.

Or say you start with:

X:\Documentation\Financials

You can create a new folder from that;
You can use that folder;
You can use any folder UNDER that folder.

You can not create a folder above that folder;
You can not use any folder above that folder.

We used that to narrow the places people could save files, as we found people would almost random save files in locations they forgot where. This way financial documents would only get saved under Financials.

Could it be broken? Yes of course. But it helped.

Note that Pointer does not have to be hard coded. It could as easily come from an inputbox. Or a textbox on a userform.

Ooooo. Post 6,666! Scary stuff.