Trying to make a macro to archive old versions if they exist before saving
Posted: Tue Oct 03, 2023 10:26 am
Hi y'all,
I'm trying to add a new function to a macro we're currently using at work.
Currently, if used on a drawing, it takes information on a Userform (Revision, Date, etc.) and creates a filename with it then saves it as a PDF in a subfolder named "PDF". What I'm trying to accomplish is, prior to saving, check if a file with the same base name already exists and, if it does, move said file to an "Archive" subfolder within the "PDF" subfolder.
Here's what I was thinking (Code is removed before and after for added clarity) :
Where the split in code is, I'd add something like this:
Anyway to make it work somehow? Any help is appreciated especially regarding the date issue.
I'm trying to add a new function to a macro we're currently using at work.
Currently, if used on a drawing, it takes information on a Userform (Revision, Date, etc.) and creates a filename with it then saves it as a PDF in a subfolder named "PDF". What I'm trying to accomplish is, prior to saving, check if a file with the same base name already exists and, if it does, move said file to an "Archive" subfolder within the "PDF" subfolder.
Here's what I was thinking (Code is removed before and after for added clarity) :
Code: Select all
Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))
If Dir(Filepath & "PDF", vbDirectory) = "" Then here
MkDir Filepath + "PDF"
End If
Filepath = Filepath + "PDF\"
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
swCustPropMgr.Get3 "Révision", False, "", Value
Filename = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
Filename = Left(Filename, Len(Filename) - 7)
If TextBox3.Value <> "" Then
theRev = "_Rev " & TextBox3.Value
End If
'
If CheckBox1.Value = True Then
Formatdate = Format(Now(), "YYYYMMDD")
Formatdate = "_" & Formatdate
End If
If TextBox1.Value <> "" Then
Pref = TextBox1.Value & "_"
End If
If TextBox2.Value <> "" Then
Suff = "_" & TextBox2.Value
End If
Code: Select all
FilenameFinal = Pref & Filename & Suff & theRev & Formatdate & ".pdf"
boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
swExportPDFData.ViewPdfAfterSaving = False
Dim swCustProp As CustomPropertyManager
Set swCustProp = swModelDocExt.CustomPropertyManager("")
boolstatus = swModelDocExt.SaveAs(Filepath & FilenameFinal, 0, 0, swExportPDFData, lErrors, lWarnings)
UserForm1.Hide
Where the split in code is, I'd add something like this:
Code: Select all
If Dir(Filepath & "Archive", vbDirectory) = "" Then ' Change Sub folder Name here
MkDir Filepath + "Archive" ' Change Sub folder Name here
End If
FilepathArchive = Filepath + "Archive\" ' Change Sub folder Name here
If IsNumeric(TextBox3.Value) = True then
Set RevExist = 0
While RevExist <= TextBox3.Value
FilenameExisting = Filename & "_Rev " & RevExist & (SOMETHING TO ACT AS A JOKER FOR ALL PREVIOUS DATES) & ".pdf"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(FilenameExisting)
Call fso.CopyFile Filepath, FilepathArchive
Else
RevExist = RevExist + 1
Wend
Else
RevNames = Array("A", "B", "C", "D", "E", "F", "G","H", "J")
For Each Revs In RevNames
FilenameExisting = Filename & "_Rev " & Revs & (SOMETHING TO ACT AS A JOKER FOR ALL PREVIOUS DATES) & ".pdf"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(FilenameExisting)
Call fso.CopyFile Filepath, FilepathArchive
Else
Next