Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 39

Thread: Solved: Allow users to set default save directory.

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Allow users to set default save directory.

    I want to create a text box where my users can save a default save directory. and when the save as dialog comes up within my program it opens up in that dialog. is this possible?

    I already know how to save it to a registry setting but how do I open the save as dialog into that folder?

    [VBA]If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    MsgBox "The Report did not Save."
    End If[/VBA]
    Last edited by Djblois; 04-27-2007 at 10:08 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Check out Cdrive and ChDir

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    ok I will look at them

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Wouldn't that be ChDrive, Bob?

    Daniel, I have an example of a UDF to Get File Name From Specific Directory. It will help in understanding the use of ChDrive and ChDir.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Ken, Bob:

    I am using this code:

    [vba]ChDrive "H:"
    ChDir "H:\dblois\My Documents\Current Projects"

    If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    MsgBox "The Report did not Save."
    End If[/vba]

    However, the save as dialog isn't starting in that directory for some reason?

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I even tested this code:

    [VBA]Dim DefaultDrive As String
    Dim DefaultDir As String

    DefaultDrive = "H"
    DefaultDir = "H:\dblois\My Documents\Current Projects"

    ChDrive (DefaultDrive)
    ChDir (DefaultDir)

    If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    MsgBox "The Report did not Save."
    End If[/VBA]

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    this opens the save as dialog for me in F:\Temp
    [VBA]Sub a()
    ChDir "f:\Temp"
    If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    End If
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Does it matter if the drive is a network drive?

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm guessing that you would have to map the network drive in windows explorer first but I'm not a network type.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thanks for trying lucas. Does anyone else know why it won't work with my network drive?

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Daniel....
    Ken's code from the link above works for networks if you change the path in the function..
    ps I didn't change it from file open to saveas as I was trying to get it to find the network drive for you:
    [vba]Option Explicit
    Private Declare Function SetCurrentDirectoryA _
    Lib "kernel32" (ByVal lpPathName As String) As Long
    Public Function GetOpenFilenameFrom(Optional sDirDefault As String) As Variant
    'Author : Ken Puls (www.excelguru.ca)
    'Macro Purpose: To ask for a file at a specified directory
    Dim sDirCurrent As String
    Dim lError As Long
    'Make note of the current directory
    sDirCurrent = "Z:\Temp"
    If sDirDefault = vbNullString Then
    'If optional arguement not supplied then
    'assign current directory as default
    sDirDefault = "Z:\Temp"
    Else
    'If option arguement is supplied, test path to ensure
    'that it exists. If not, assign current directory
    If Len(Dir(sDirDefault, vbDirectory)) = 0 Then
    sDirDefault = sDirCurrent
    End If
    End If
    'Change the drive and directory
    '*Drive change is unecessary if same, but takes as long to test
    ' as just changing it
    If Not Left(sDirDefault, 2) = "\\" Then
    'Not a network drive, so use ChDir
    ChDrive Left(sDirDefault, 1)
    ChDir (sDirDefault)
    Else
    'Network drive, so use API
    lError = SetCurrentDirectoryA(sDirDefault)
    If lError = 0 Then _
    MsgBox "Sorry, I encountered an error accessing the network file path"
    ChDir (sDirDefault)
    End If
    'Get the file's name & path, setting the filters to only display
    'desired types. Help on the exact syntax can be found by looking
    'up the GetOpenFilename method in the VBA help files
    GetOpenFilenameFrom = Application.GetOpenFilename _
    ("Excel Files (*.xl*), *.xl*,All Files (*.*),*.*")
    'Change the drive and directory back
    If Not Left(sDirCurrent, 2) = "\\" Then
    'Not a network drive, so use ChDrive
    ChDrive Left(sDirCurrent, 1)
    ChDir (sDirCurrent)
    Else
    'Network drive, so use API
    lError = SetCurrentDirectoryA(sDirCurrent)
    If lError = 0 Then _
    MsgBox "Sorry, I encountered an error resetting the network file path"
    ChDir (sDirCurrent)
    End If
    End Function
    Sub GetMeAFile()
    'Author : Ken Puls (www.excelguru.ca)
    'Macro Purpose: To test the GetOpenFilenameFrom function
    Dim sWBToOpen As Variant
    sWBToOpen = GetOpenFilenameFrom(Range("A3").Value)

    If Not sWBToOpen = False Then Workbooks.Open (sWBToOpen)

    End Sub[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    ok, i looked at that code. I just didn't understand it, I guess I will have to look at it harder. thank you.

  13. #13
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Ken, Lucas:

    this is what I got the code up to and logically it looks like it should work but it doesn't. I guess I don't know enough about VBA yet.

    [VBA]Option Explicit
    Private Declare Function SetCurrentDirectoryA _
    Lib "kernel32" (ByVal lpPathName As String) As Long
    Public Function GetSaveFileDir(Optional sDirDefault As String) As Variant
    'Author : Ken Puls (www.excelguru.ca)
    'Macro Purpose: To ask for a file at a specified directory
    Dim sDirCurrent As String
    Dim lError As Long
    'Make note of the current directory
    sDirCurrent = "H:\\Dblois"
    If sDirDefault = vbNullString Then
    'If optional arguement not supplied then
    'assign current directory as default
    sDirDefault = "H:\\Dblois"
    Else
    'If option arguement is supplied, test path to ensure
    'that it exists. If not, assign current directory
    If Len(Dir(sDirDefault, vbDirectory)) = 0 Then
    sDirDefault = sDirCurrent
    End If
    End If
    'Change the drive and directory
    '*Drive change is unecessary if same, but takes as long to test
    ' as just changing it
    If Not Left(sDirDefault, 2) = "\\" Then
    'Not a network drive, so use ChDir
    ChDrive Left(sDirDefault, 1)
    ChDir (sDirDefault)
    Else
    'Network drive, so use API
    lError = SetCurrentDirectoryA(sDirDefault)
    If lError = 0 Then _
    MsgBox "Sorry, I encountered an error accessing the network file path"
    ChDir (sDirDefault)
    End If

    End Function
    Sub testcode()

    Dim sDirToSaveTo As Variant
    sDirToSaveTo = GetSaveFileDir
    If Not sDirToSaveTo = False Then ChDir (sDirToSaveTo)
    If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    MsgBox "The Report did not Save."
    End If

    End Sub[/VBA]

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm not sure either Daniel because it does work for me....only thing I can see would be a path problem....are you sure of it?

    ahh, I just noticed something...in the function you have 2 backslashes in your path...
    [vba]sDirDefault = "H:\\Dblois"[/vba]
    try removing one of them..
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I originally tried with only one \ but looking at Ken's code, I thought maybe Network drives needed 2 \\:

    [VBA]If Not Left(sDirDefault, 2) = "\\" Then
    'Not a network drive, so use ChDir
    [/VBA]

  16. #16
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Now I am testing this:

    [vba]Sub testcode()

    Dim sDirToSaveTo As Variant
    Dim sDirCurrent As String
    Dim lError As Long
    'Make note of the current directory
    sDirCurrent = "H:\Dblois"
    If sDirDefault = vbNullString Then
    'If optional arguement not supplied then
    'assign current directory as default
    sDirDefault = "H:\Dblois"
    Else
    'If option arguement is supplied, test path to ensure
    'that it exists. If not, assign current directory
    If Len(Dir(sDirDefault, vbDirectory)) = 0 Then
    sDirDefault = sDirCurrent
    End If
    End If
    'Change the drive and directory
    '*Drive change is unecessary if same, but takes as long to test
    ' as just changing it
    If Not Mid(sDirDefault, 3, 2) = "\\" Then
    'Not a network drive, so use ChDir
    ChDrive Left(sDirDefault, 1)
    ChDir (sDirDefault)
    Else
    'Network drive, so use API
    'lError = SetCurrentDirectoryA(sDirDefault)
    If lError = 0 Then _
    MsgBox "Sorry, I encountered an error accessing the network file path"
    ChDir (sDirDefault)
    End If
    sDirToSaveTo = sDirDefault
    If Not sDirToSaveTo = False Then ChDir (sDirDefault)
    If Not Application.Dialogs(xlDialogSaveAs).Show(, 1) Then
    MsgBox "The Report did not Save."
    End If[/vba]

    It still won't work? Ken since it is your code maybe you know what I am doing wrong?

  17. #17
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    So big question... how does the drive display in windows explorer? Is it H:\dblois, or is it \\servername\dblois?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  18. #18
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Here you go Ken, copied right out of Explorer:

    H:\dblois

  19. #19
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I just copied your most recent code, and tried testing with both of the following directories which are valid on my system:
    J:\vba tests
    C:\tmp

    Both launched just fine.

    What errors are you getting, if any? Does it stop on a line when you try to step through? Are you watching the values being set in the locals window to ensure that they are correct?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  20. #20
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I have watched them in the watch window and I am not getting any errors. It is just running through and opening into the save as dialog but not in my directory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •