AnsweredAssumed Answered

Macro to change sheet names

Question asked by Dick Timmerman on Dec 7, 2020
Latest reply on Dec 9, 2020 by John Stoltzfus

Hi, i'm quite new here on the forum, however already about 20 yrs of experience with Solidworks, almost 30 yrs with 3D CAD. At his moment I'm struggling with some Drawing Template issues. Due to the fact that the Standard SW property "SW-BOM Part Number(BOM Part Number)" is not showing what I would and should expect started to work with macro's. The problem is as follows: I have an assembly on sheet 1, all of my components on separate sheets in the same drawing. That's just to save part number info on mono drawings because we will never have these underlaying parts in the warehouse, only in the finished assembly. In my template, for the title i'm now looking at the property "SW-Sheet Name(Sheet Name)". Therefore I have to rename my sheets. Sofar I was able to get all the things working as it should, On-save, On-change, On-open, the macro runs automatically. Made a Listener event, can start the listener automatically via the start option /m. 

Now, when I his morning SW, opened a Drawing, the renaming does it's work however, not completely. So I'm searching where it goes wrong. Following code, made by myself and used some of the on the internet avalable examples.

 

Sub main()
Set swApp = Application.SldWorks
Set Model = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set swDraw = Model
vSheetName = swDraw.GetSheetNames

FilePath = Part.GetPathName

Newsheetname = Mid(FilePath, InStrRev(FilePath, "\ST") + 1, 8)

For i = 0 To UBound(vSheetName)
swDraw.ActivateSheet vSheetName(i)
Set myDrawingSheet = swDraw.GetCurrentSheet
If i = 0 Then
myDrawingSheet.SetName Newsheetname
ElseIf i > 8 Then
myDrawingSheet.SetName "#TEMP" & Newsheetname & "-" & i
Else
myDrawingSheet.SetName "#TEMP" & Newsheetname & "-0" & i
End If
Next i

vSheetName = swDraw.GetSheetNames
For i = 0 To UBound(vSheetName)
swDraw.ActivateSheet vSheetName(i)
Set myDrawingSheet = swDraw.GetCurrentSheet
myDrawingSheet.SetName Replace(vSheetName(i), "#TEMP", "")
Next i

swDraw.ActivateSheet vSheetName(0)

End Sub

 

Anyone why SW doesn't perform the last "replace" correct?

Outcomes