Consulting

Results 1 to 5 of 5

Thread: how to get the directory (file save) when the macro is run the first time in the day

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    5
    Location

    how to get the directory (file save) when the macro is run the first time in the day

    Hello everyone; I wonder if you can help me with a specific issue:


    On the first run (of the day):

    newfilename = Application.GetSaveAsFilename(InitialFileName:="xyz001.xlsx", FileFilter:="Excel Workbook (*.xlsx), *.xlsx", Title:="Save As")
    If newfilename = False Then Exit Sub
    ActiveWorkbook.SaveAs Filename:=newfilename
    I would like to get the directory path so that in all the next saves (say till xyz999.xlsx) I can bring up the same directory path and the next file number for
    convenience without having the user select the path every time the macro is run. (Not just for convenience but I need the directory to figure out the next file number)

    Thank you.
    Last edited by SamT; 01-29-2016 at 06:46 AM. Reason: Added CODE Tags with Editor's # Icon

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    In a standard Module
    Option Explicit
    
    Dim mPath As String
    Dim mNameNumber As String
    
    Public Function NameNumber() As String
    If mNameNumber = "" Then mNameNumber = "001"
    NameNumber = mNameNumber
    mNameNumber = Format(CLng(mNameNumber) + 1, "000")
    End Function
    Modify your code like this
    Sub t()
    Dim newfilename
    If mPath = "" Then
    newfilename = Application.GetSaveAsFilename(InitialFileName:="xyz" & NameNumber & ".xlsx", FileFilter:="Excel Workbook (*.xlsx), *.xlsx", Title:="Save As")
    mPath = Left(newfilename, Len(newfilename) - InStrRev(newfilename, "\"))
    Else
    newfilename = Application.GetSaveAsFilename(InitialFileName:=mPath & "xyz" & NameNumber & ".xlsx", FileFilter:="Excel Workbook (*.xlsx), *.xlsx", Title:="Save As")
    End If
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    5
    Location
    SamT thank you so much for your help and clear code. Global declaration of the variables dawned on me 6 hours or so after my post! The global variables keep their values until the application is closed which is great. When user starts Excel the next day it will suggest file number 001 again but I guess there's not much one can do to overcome that. I changed the first part of the code a bit to accommodate for the Cancel button:
    Option Explicit
     
    Dim mPath As String
    Dim nextFilename As String
    Dim newfilename
    Sub t()    
        If mPath = "" Then
            newfilename = Application.GetSaveAsFilename(InitialFileName:="xyz001.xlsx", FileFilter:="Excel Workbook (*.xlsx), *.xlsx", Title:="Save As")
            mPath = Left(newfilename, InStrRev(newfilename, "\"))
            If newfilename = False Then Exit Sub
            ActiveWorkbook.SaveAs Filename:=newfilename
        Else
            Call NextFile
            newfilename = Application.GetSaveAsFilename(InitialFileName:=nextFilename, FileFilter:="Excel Workbook (*.xlsx), *.xlsx", Title:="Save As")
            If newfilename = False Then Exit Sub
            ActiveWorkbook.SaveAs Filename:=newfilename
        End If
    End Sub
    I had made the NextFile subroutine for figuring out the next file number based on 1)most recently saved/modified file in that format in that folder or 2) the highest number of the file in that format in that folder.
    That way teher is no chance of overwriting an already saved file.

    All I need now is a way to auto click save after 3 seconds of inactivity when the Save as dialog box is displayed (with the suggested next filename) and of course when the user clicks the filename area the timer should stop.

    Thank you.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    When user starts Excel the next day it will suggest file number 001 again but I guess there's not much one can do to overcome that.
    You could save the last sequence number and the folder in the registry
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Put the "Saving" code in a UserForm with a timer. When the SaveAs Dialog is shown Load, but don't show the UserForm. When the Dialog is clicked run the SaveAs Sub in the UserForm and Unload the form.

    I thimk.

    When user starts Excel the next day it will suggest file number 001 again but I guess there's not much one can do to overcome that
    Create a Project.ini file in the Excel Folder and use it like a CSV file

    FileNamePart, LastPath, LastNum
    xyz, C;\MyDir, 001


    See this thread for speedy handling of CSVs: Process All CSV Files In SubFolders
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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