John Riggs

'Save As' to initiate a macro

Discussion created by John Riggs on Jul 15, 2019
Latest reply on Jul 15, 2019 by John Riggs

Hello All, 


I'm attempting to have a macro be initiated by saving a drawing. I've done some research and found a forum of how to do it in excel but with my extremely limited macro knowledge, I am unable to convert the code to work in Solidworks.


The code for excel I found is:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) For x = 1 To Sheets.Count If Worksheets(x).Range("A1").Value <> "" Then Sheets(x).Name = Worksheets(x).Range("A1").Value End If Next


Below is the macro that I already have. 


Any help or direction would be greatly appreciated. 





Sub main()
Dim swdoc As SldWorks.ModelDoc2
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swAnn As SldWorks.Annotation
Dim vSheetNames As Variant
Dim vSheetName As Variant
Dim vAnns As Variant
Dim vAnn As Variant
Dim swWeldSymbol As SldWorks.WeldSymbol
Dim BottomSymbolName As String
Dim topfinishingName As String
Dim bottomfinishingmethod As String
Dim TopSymbolName As String
Dim DisplayWarning As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
vSheetNames = swDraw.GetSheetNames
For Each vSheetName In vSheetNames
swDraw.ActivateSheet vSheetName
Set swView = swDraw.GetFirstView
While Not swView Is Nothing
vAnns = swView.GetAnnotations
If Not IsEmpty(vAnns) Then
For Each vAnn In vAnns
Set swAnn = vAnn
If swAnn.GetType = swAnnotationType_e.swWeldSymbol Then
Set swWeldSymbol = swAnn.GetSpecificAnnotation
BottomSymbolName = swWeldSymbol.GetText(swWeldSymbolTextTypes_e.swWeldSymbolTextBelow)
TopSymbolName = swWeldSymbol.GetText(swWeldSymbolTextTypes_e.swWeldStaggerTextAbove)
If TopSymbolName <> "" Then Debug.Print " Symbol name above: " & TopSymbolName
If BottomSymbolName <> "" Then Debug.Print " Symbol name below: " & BottomSymbolName
If BottomSymbolName = "<AWLD-GSQ>" Or TopSymbolName = "<AWLD-GSQ>" Or BottomSymbolName = "<AWLD-PLUG>" Or TopSymbolName = "<AWLD-PLUG>" Then
swAnn.Select3 bottomfinishingmethod = "g-grinding", Nothing
DisplayWarning = True
End If
End If
End If
Set swView = swView.GetNextView
If DisplayWarning Then MsgBox ("CAUTION: Add Grinding or Confirm weld thickness WILL NOT damage adjoining surface!")

End Sub