Bananaman3350

Auto Save Filename Macro v1

Aug 27th, 2025
2,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Option Explicit
  2.  
  3. Dim docEvents As clsDocEvents
  4.  
  5. Const csvFilename As String = "auto_filename.csv"
  6. Const NMB_FORMAT As String = "0000" 'padding for the number, e.g. 001, 002, instead of 1, 2
  7.  
  8. Sub AutoSetPartTitle()
  9.     Dim swApp As SldWorks.SldWorks
  10.     Dim swModel As ModelDoc2
  11.     Dim templatePath As String
  12.     Dim csvPath As String
  13.     Dim fso As Object
  14.     Dim FileContent As String
  15.     Dim FileLines() As String
  16.     Dim validLines() As String
  17.     Dim csvData() As Variant
  18.     Dim i As Long, j As Long
  19.     Dim selectedIndex As Long
  20.     Dim newNumber As Long
  21.     Dim newTitle As String
  22.     Dim updatedCSV As String
  23.     Dim colCount As Long
  24.     Dim validCount As Long
  25.     Dim originalSavePath As String
  26.  
  27.     Set swApp = Application.SldWorks
  28.     Set swModel = swApp.ActiveDoc
  29.     If swModel Is Nothing Then Exit Sub
  30.  
  31.     ' Get Document Templates path
  32. '    templatePath = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
  33. '    csvPath = Left(templatePath, InStrRev(templatePath, "\")) & csvFilename
  34.    csvPath = "Y:\~Solidworks Templates\" & csvFilename
  35.  
  36.     ' Read entire CSV file into a string
  37.    Set fso = CreateObject("Scripting.FileSystemObject")
  38.     If Not fso.FileExists(csvPath) Then
  39.         MsgBox "CSV file not found: " & csvPath, vbCritical
  40.         Exit Sub
  41.     End If
  42.  
  43.     With fso.OpenTextFile(csvPath, 1)
  44.         FileContent = .ReadAll
  45.         .Close
  46.     End With
  47.  
  48.     ' Split into lines
  49.    FileLines = Split(FileContent, vbCrLf)
  50.     FileContent = ""
  51.    
  52.     ' First, count valid (non-blank) lines
  53.    validCount = 0 'non-zero based
  54.    For i = 0 To UBound(FileLines)
  55.         If Len(Trim(FileLines(i))) > 0 Then
  56.             ReDim Preserve validLines(0 To validCount)
  57.             validLines(validCount) = FileLines(i)
  58.             validCount = validCount + 1
  59.         End If
  60.     Next i
  61.    
  62.     Erase FileLines
  63.    
  64.     ' Exit if no valid data
  65.    If validCount = 0 Then
  66.         MsgBox "CSV file is empty or contains only blank lines.", vbCritical
  67.         Exit Sub
  68.     End If
  69.  
  70.     ' Determine column count from first valid line
  71.    colCount = UBound(Split(validLines(0), ",")) 'zero based
  72.  
  73.     ' Populate csvData with only valid rows
  74.    ReDim csvData(0 To validCount - 1, 0 To colCount)
  75.     For i = 0 To validCount - 1
  76.         Dim fields() As String
  77.         fields = Split(validLines(i), ",")
  78.         For j = 0 To colCount
  79.             If j <= UBound(fields) Then
  80.                 csvData(i, j) = Trim(fields(j))
  81.             Else
  82.                 csvData(i, j) = ""
  83.             End If
  84.         Next j
  85.     Next i
  86.    
  87.     Erase fields
  88.     Erase validLines
  89.    
  90.     ' Show form (skip header row)
  91.    frmPrefixSelector.lstPrefixes.Clear
  92.     For i = 1 To UBound(csvData, 1)
  93.         If Trim(csvData(i, 0)) <> "" Then
  94.             frmPrefixSelector.lstPrefixes.AddItem csvData(i, 0) ' Prefix column
  95.        End If
  96.     Next i
  97.  
  98.     frmPrefixSelector.Show
  99.     selectedIndex = frmPrefixSelector.lstPrefixes.ListIndex
  100.     If selectedIndex < 0 Then Exit Sub
  101.  
  102.     ' Adjust index to match csvData (skip header)
  103.    selectedIndex = selectedIndex + 1
  104.  
  105.     ' Generate new title
  106.    newNumber = CLng(csvData(selectedIndex, 1)) + 1
  107.     newTitle = csvData(selectedIndex, 0) & Format(newNumber, NMB_FORMAT)
  108.     swModel.SetTitle2 newTitle
  109.  
  110.     ' Update CSV array
  111.    csvData(selectedIndex, 1) = CStr(newNumber)
  112.  
  113.     ' Write updated CSV including header
  114.    With fso.CreateTextFile(csvPath, True)
  115.         For i = 0 To UBound(csvData, 1)
  116.             If Trim(csvData(i, 0)) <> "" Then
  117.                 updatedCSV = ""
  118.                 For j = 0 To colCount
  119.                     updatedCSV = updatedCSV & csvData(i, j)
  120.                     If j < colCount Then updatedCSV = updatedCSV & ","
  121.                 Next j
  122.                 .WriteLine updatedCSV
  123.             End If
  124.         Next i
  125.         .Close
  126.     End With
  127.    
  128.     Dim targetPath As String
  129.     targetPath = csvData(selectedIndex, 2) & "\" & swModel.GetTitle & GetFileExtension(swModel)
  130.  
  131.     originalSavePath = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swFileLocationsDefaultSave)
  132.     swApp.SetUserPreferenceStringValue swUserPreferenceStringValue_e.swFileLocationsDefaultSave, csvData(selectedIndex, 2)
  133.  
  134.    
  135.     ' Hook into document events
  136.    Set docEvents = New clsDocEvents
  137.     docEvents.Init swModel, swApp, originalSavePath
  138.  
  139.  
  140.     'swApp.SetUserPreferenceStringValue swUserPreferenceStringValue_e.swFileLocationsDefaultSave, csvData(selectedIndex, 2)
  141.    MsgBox "Title set to: " & newTitle, vbInformation
  142.    
  143. End Sub
  144.  
  145. Function GetFileExtension(doc As ModelDoc2) As String
  146.     Dim docType As Long
  147.     docType = doc.GetType
  148.     Select Case docType
  149.         Case swDocPART
  150.             GetFileExtension = ".sldprt"
  151.         Case swDocASSEMBLY
  152.             GetFileExtension = ".sldasm"
  153.         Case swDocDRAWING
  154.             GetFileExtension = ".slddrw"
  155.         Case Else
  156.             GetFileExtension = ""
  157.     End Select
  158. End Function
Advertisement
Add Comment
Please, Sign In to add comment