View Full Version : Automatic File Naming (Date, Version)
vebea
01-02-2018, 01:09 PM
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
Paul_Hossler
01-02-2018, 01:48 PM
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
vebea
01-03-2018, 02:53 AM
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.
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
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!
John Wilson
01-03-2018, 04:32 AM
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
vebea
01-03-2018, 07:51 AM
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!
John Wilson
01-03-2018, 08:26 AM
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
vebea
01-03-2018, 09:37 AM
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 :(
John Wilson
01-03-2018, 10:03 AM
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
RayKay
12-14-2018, 03:14 AM
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.
:clap:
John Wilson
12-14-2018, 08:00 AM
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
RayKay
12-14-2018, 08:11 AM
Great, thanks! I wish I had your skillsbase!
Thank you :clap:
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.