Consulting

Results 1 to 11 of 11

Thread: Automatic File Naming (Date, Version)

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    4
    Location

    Automatic File Naming (Date, Version)

    Hi all,

    I want to code a makro for automatic naming and versioning. The format for a file is YYYY-MM-DD_file_V1.pptx

    My code works for presentations which are named without any date, e.g. mypresentation.pptx -> This will be named into 2018-01-02_mypresentation_V01.pptx. (Actually the V1 part is currently NOT working, the V1 suffix is added after the .pptx)

    If you save the file (2018-01-02_mypresentation_V1.pptx) again, the file will be saved as 2018-01-02_ion.pptxV1_ due to the wrong usage of "Mid..."

    I think of repairing my code by the use of variables and if Loops.Or I might use sth like
    strDotPosition = InStrRev(ActivePresentation.Name, ., [-1], [vbTextCompare]) to find the dot from .pptx.
    Maybe somebody can give me a good hint/workaround, how to fix my code in a nice, smart way.


    My code so far:

    Sub SaveAsButton()
    
        Dim saveAs As FileDialog
        Dim strFileName As String
    '    Dim strVersionNumber As String
        Dim strDotPosition As String
    
    strVersionNumber = 0
      
      Set saveAs = Application.FileDialog(msoFileDialogSaveAs)
            
    With saveAs
                .Title = "Save as"
                .InitialView = msoFileDialogViewList
                .InitialFileName = Format(Now, "yyyy-mm-dd") & "_" & Mid( _
                         ActivePresentation.Name, 12) & "V" & _
                         strVersionNumber + 1 & "_" & ".bas"
                
    If .Show = -1 Then
                    strFileName = .SelectedItems(1)
                    ActivePresentation.saveAs (strFileName)
                    MsgBox "File was succesfully saved."
               
                Else
                     MsgBox "Saving canceled."
                 
                End If
            End With
        Set saveAs = Nothing
    End Sub
    Last edited by Paul_Hossler; 01-02-2018 at 01:39 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. I added the [ CODE ] tags around your macro for you to format it; you can use the [#] icon to insert the tags and paste your macro between them next time

    2. Question: Is this your workflow?

    a. Create a presentation and save it as "mypres.pptm" on Monday (1/1/2018)

    b. Open it on Tuesday and it saves as "2018-01-02_mypres_v1.pptm"

    c. Open it again on Tuesday and it saves as "2018-01-02_mypres_v2.pptm"

    d. Open it on Wednesday (1/3/2018) and it saves as
    "2018-01-02_mypres_v3.pptm" OR as --- Same date, bump version
    "2018-01-03_mypres_v1.pptm" OR as --- New date, restart version
    "2018-01-03_mypres_v3.pptm" --- New date, bump version
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    4
    Location
    Quote Originally Posted by Paul_Hossler View Post
    1. I added the [ CODE ] tags around your macro for you to format it; you can use the [#] icon to insert the tags and paste your macro between them next time
    Thanks for your hint! I will use these tags in future.

    Quote Originally Posted by Paul_Hossler View Post
    2. Question: Is this your workflow?

    a. Create a presentation and save it as "mypres.pptm" on Monday (1/1/2018)

    b. Open it on Tuesday and it saves as "2018-01-02_mypres_v1.pptm"

    c. Open it again on Tuesday and it saves as "2018-01-02_mypres_v2.pptm"
    There are two different workflows. 1) create a new presentation (mypres.pptm), which has to be named correctly. 2) second workflow would be, to open a file which was already named correctly, and adapt its date and version

    Quote Originally Posted by Paul_Hossler View Post
    d. Open it on Wednesday (1/3/2018) and it saves as
    "2018-01-02_mypres_v3.pptm" OR as --- Same date, bump version
    "2018-01-03_mypres_v1.pptm" OR as --- New date, restart version
    "2018-01-03_mypres_v3.pptm" --- New date, bump version
    This is exactly what I tried to explain with the second workflow. 2018-01-02_mypres_v2.pptm should be automatically named to 2018-01-03_mypres_v3.pptm (New date, bump version)

    So from my point of view, I have issues coding these two workflows and especially combining them together. The code as described, should work for files without correct naming and for files which are already tagged with a date and version.

    Thanks for your help!

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    See if anything here helps. START with a non versioned file.

    Sub SaveAsButton()
        Dim FD As FileDialog
        Dim opres As Presentation
        Dim rayNAME() As String
        Dim lngVERSION As Long
        Dim strFileName As String
    
    
        Set opres = ActivePresentation
        If opres.Path = "" Then
            MsgBox "You must manually save this unsaved presentation!", vbCritical
            Exit Sub
        End If
        If opres.Tags("NAME") = "" Then opres.Tags.Add "NAME", opres.Name
        If opres.Tags("VERSION") = "" Then opres.Tags.Add "VERSION", "1"
        rayNAME = Split(opres.Tags("NAME"), ".")
        lngVERSION = CStr(opres.Tags("VERSION"))
        strFileName = Format(Now, "yyyy-mm-dd") & "_" & rayNAME(0) & "V_" & Format(CStr(lngVERSION), "0#")
        Set FD = Application.FileDialog(msoFileDialogSaveAs)
        With FD
            .Title = "Save as"
            .InitialView = msoFileDialogViewList
            .InitialFileName = strFileName '
            .FilterIndex = 2
            If .Show = -1 Then
                strFileName = .SelectedItems(1)
                ActivePresentation.saveAs (strFileName)
                MsgBox "File was succesfully saved."
                lngVERSION = lngVERSION + 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
            Else
                MsgBox "Saving canceled."
            End If
        End With
        Set FD = Nothing
    End Sub
    Last edited by John Wilson; 01-03-2018 at 05:19 AM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    4
    Location
    Quote Originally Posted by John Wilson View Post
    Format(CStr(lngVERSION), "0#")
    Thanks John, nice work! However I've found a bug which I can't resolve by myself.
    If you save a file on a different date, the version will stay the same. For example "2018-01-02_pres_V04" will be named "2018-01-03_pres_V04", but I need it to be named ""2018-01-03_pres_V05". Can you please help me out which part of the code has to be adjusted?

    Many thanks mate!

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Actually it's not the date change. Silly error the VERSION is updated after the save so if you close it will be lost!

    Try this instead

    Sub SaveAsButton()
        Dim FD As FileDialog
        Dim opres As Presentation
        Dim rayNAME() As String
        Dim lngVERSION As Long
        Dim strFileName As String
    
    
        Set opres = ActivePresentation
        If opres.Path = "" Then
            MsgBox "You must manually save this unsaved presentation!", vbCritical
            Exit Sub
        End If
        'update version BEFORE save
    
    
        lngVERSION = CStr(opres.Tags("VERSION"))
        lngVERSION = lngVERSION + 1
        opres.Tags.Add "VERSION", CStr(lngVERSION)
        If opres.Tags("NAME") = "" Then opres.Tags.Add "NAME", opres.Name
        If opres.Tags("VERSION") = "" Then opres.Tags.Add "VERSION", "1"
        rayNAME = Split(opres.Tags("NAME"), ".")
    
    
        strFileName = Format(Now, "yyyy-mm-dd") & "_" & rayNAME(0) & "V_" & Format(CStr(lngVERSION), "0#")
        Set FD = Application.FileDialog(msoFileDialogSaveAs)
    
    
        With FD
            .Title = "Save as"
            .InitialView = msoFileDialogViewList
            .InitialFileName = strFileName    '
            .FilterIndex = 2
            If .Show = -1 Then
                strFileName = .SelectedItems(1)
                ActivePresentation.saveAs (strFileName)
                MsgBox "File was succesfully saved."
            Else
                MsgBox "Saving canceled."
                ' roll back version if not saved
                lngVERSION = lngVERSION - 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
            End If
        End With
        Set FD = Nothing
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  7. #7
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    4
    Location
    Quote Originally Posted by John Wilson View Post
    Actually it's not the date change. Silly error the VERSION is updated after the save so if you close it will be lost!

    Try this instead

        lngVERSION = CStr(opres.Tags("VERSION"))
    Unfortunately I receive a runtime error (13) in that line of code

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Yep you need to make the tag BEFORE using it!

    Maybe this

    Sub SaveAsButton()    Dim FD As FileDialog
        Dim opres As Presentation
        Dim rayNAME() As String
        Dim lngVERSION As Long
        Dim strFileName As String
    
    
        Set opres = ActivePresentation
        If opres.Path = "" Then
            MsgBox "You must manually save this unsaved presentation!", vbCritical
            Exit Sub
        End If
       
    
    
        If opres.Tags("NAME") = "" Then opres.Tags.Add "NAME", opres.Name
        If opres.Tags("VERSION") = "" Then opres.Tags.Add "VERSION", "0"
        rayNAME = Split(opres.Tags("NAME"), ".")
          lngVERSION = CStr(opres.Tags("VERSION"))
     lngVERSION = lngVERSION + 1
     opres.Tags.Add "VERSION", CStr(lngVERSION)
        strFileName = Format(Now, "yyyy-mm-dd") & "_" & rayNAME(0) & "V_" & Format(CStr(lngVERSION), "0#")
        Set FD = Application.FileDialog(msoFileDialogSaveAs)
    
    
        With FD
            .Title = "Save as"
            .InitialView = msoFileDialogViewList
            .InitialFileName = strFileName    '
            .FilterIndex = 2
            If .Show = -1 Then
                strFileName = .SelectedItems(1)
                ActivePresentation.saveAs (strFileName)
                MsgBox "File was succesfully saved."
                lngVERSION = lngVERSION + 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
    
    
            Else
                MsgBox "Saving canceled."
                lngVERSION = lngVERSION - 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
            End If
        End With
        Set FD = Nothing
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location
    Hi John, wonderful code, but each time I save a file it goes v.01 to v.03 to v.05 to v.07 etc. Always jumping 2 versions. Any change I need to make please? I've experimented, but failed

    Also, is there a way to put the date & version AFTER the filename? And possibly a version with the date? That would be great

    Thanks for your time in advance.

  10. #10
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I fixed the version bug. You just need to adapt the first strFileName line to get what you need.

    Sub SaveAsButton()
    Dim FD As FileDialog
        Dim opres As Presentation
        Dim rayNAME() As String
        Dim lngVERSION As Long
        Dim strFileName As String
    
    
        Set opres = ActivePresentation
        If opres.Path = "" Then
            MsgBox "You must manually save this unsaved presentation!", vbCritical
            Exit Sub
        End If
       
        If opres.Tags("NAME") = "" Then opres.Tags.Add "NAME", opres.Name
        If opres.Tags("VERSION") = "" Then opres.Tags.Add "VERSION", "0"
        rayNAME = Split(opres.Tags("NAME"), ".")
          lngVERSION = CStr(opres.Tags("VERSION"))
     opres.Tags.Add "VERSION", CStr(lngVERSION)
        strFileName = rayNAME(0) & "_" & Format(Now, "yyyy-mm-dd") & "_" & "V_" & Format(CStr(lngVERSION), "0#")
        Set FD = Application.FileDialog(msoFileDialogSaveAs)
    
    
        With FD
            .Title = "Save as"
            .InitialView = msoFileDialogViewList
            .InitialFileName = strFileName    '
            .FilterIndex = 2
            If .Show = -1 Then
                strFileName = .SelectedItems(1)
                ActivePresentation.SaveAs (strFileName)
                MsgBox "File was succesfully saved."
                lngVERSION = lngVERSION + 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
            Else
                MsgBox "Saving canceled."
                lngVERSION = lngVERSION - 1
                opres.Tags.Add "VERSION", CStr(lngVERSION)
            End If
        End With
        Set FD = Nothing
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  11. #11
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location
    Great, thanks! I wish I had your skillsbase!
    Thank you

Posting Permissions

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