Edit macro crash SW

Hello !

I have modified a macro in SW 2015 SP4, it was not completed but it worked for me. I saved it and now when I try to edit, SW crash. I can edit other macro without any issues.There is someone here who can try to open it in another version.If it works, sending me a text version of the code would be really appreciated!

SolidworksApi macros
Answers 4
CC
CC 2015-11-13 Best Answer

I can open your macro without a problem

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim cwApp As CAMWORKSLib.cwApp
    Dim cwDoc As CAMWORKSLib.cwDoc
    Dim PostFileName As String
    Dim strSetupName As String
    Dim docTitle As String
    Dim nNumSetups As Integer
    Dim nErrors As Long
    Dim nWarnings As Long
    Dim blTlpGen As Boolean
   
    'Get Solidworks App
    Set swApp = Application.SldWorks

    'Get Active Model Doc
    Set swModel = swApp.ActiveDoc
    Set cwApp = New CAMWORKSLib.cwApp
    Set cwDoc = cwApp.IGetActiveDoc
    Set PartDoc = cwDoc
    Set CWMachine = PartDoc.IGetMachine
   
    'Document Title
    Set swPart = swApp.ActiveDoc
    Set swModel = swApp.ActiveDoc
    docTitle = swModel.GetTitle
    If InStr(docTitle, " ") > 0 Then
       docTitle = Replace(docTitle, " ", "_")
    End If
    Set cwMach = cwDoc.IGetMachine
   
    'Renumber all setups if not named
   
    Set setups = cwMach.IGetEnumOpSetups
    nNumSetups = setups.Count
    For i = 0 To setups.Count - 1
       Set setup = setups.Item(i)
       strSetupName = setup.GetName
       Dim lngSetupNameLength As Integer
       lngSetupNameLength = Len(strSetupName)
       If lngSetupNameLength > 5 Then setup.SetName (i + 1)
    Next i
   
    'Set Letter
    Dim Letter As String
    If nNumSetups > 1 Then
        If Not IsNumeric(Right(docTitle, 1)) Then
            Letter = "1"
        Else
            Letter = "A"
        End If
    Else
        Letter = ""
    End If
   
    'Post Process
    'MW_SELNSET_TYPE_MILL_OPSETUPS = 7
   
    'Method to get all selected operations
    Dim z%
    'Dim setups As CAMWORKSLib.CWDispatchCollection
    Dim selectionMgr As CAMWORKSLib.CWSelectionMgr
    Dim nSelectionType, nBrowserType As Long
    Set selectionMgr = cwDoc.IGetSelectionMgr
        
    nSelectionType = 7
    nBrowserType = 1
    Set setups = selectionMgr.GetSelectionsOfType(7, 1)   'get selected setups from "operation tab"
    If setups Is Nothing Then
        MsgBox "No Setup selected"
        GoTo test
    End If
    'nseloperations = setups.Count
   
    MsgBox (setups.Count) & " Setup selected"
    For z = 0 To setups.Count - 1
        'Set lstSelSetups(z) = setups.Item(z)
        MsgBox "This is Setup " & z + 1
    Next z
    GoTo test
   
   
   
   
   
    Set setups = cwMach.IGetEnumOpSetups
    For j = 0 To setups.Count - 1
        Set setup = setups.Item(j)
        Set operations = setup.IGetEnumOperations
       
        If setup.GetSuppress = True Or operations.Count = 0 Then GoTo postskip
            For k = 0 To operations.Count - 1
                Set operation = operations.Item(k)
                If operation.GetIsToolpathGenerated = True Then blTlpGen = True
            Next k
        If blTlpGen = True Then setup.GenerateToolpath
        If blTlpGen = False And cwMach.MachType <> CW_MACHINE_TYPE_TURN Then GoTo postskip
        blTlpGen = False
        If cwMach.Name = "Takumi MV15" Then
           Call CWMachine.SetController("FANUC18I.ctl", "C:\CAMWorksData\Posts\FANUC18I.ctl")
           docpath = "\\PRBDC01\DESSINS\CNC\NETWORK\takumiv15" & "\"
           PostFileName = docpath & docTitle & Letter & ".N"
        End If
        If cwMach.Name = "Takumi VC0851" Then
           Call CWMachine.SetController("FANUC18I.ctl", "C:\CAMWorksData\Posts\FANUC18I.ctl")
           docpath = "\\PRBDC01\DESSINS\CNC\NETWORK\takumivc0851" & "\"
           PostFileName = docpath & docTitle & Letter & ".N"
        End If
        If cwMach.Name = "Maximart Mill" Then
           Call CWMachine.SetController("ANILAM.ctl", "C:\CAMWorksData\Posts\ANILAM.ctl")
           docpath = "\\Nas-prb\maxi" & "\"
           Dim lngNumberOfCharacters As Long
           lngNumberOfCharacters = Len(docTitle)
           If lngNumberOfCharacters = 7 Then docTitle = Right(docTitle, 5)
           If lngNumberOfCharacters = 8 Then docTitle = Right(docTitle, 6)
           If lngNumberOfCharacters = 9 Then docTitle = Right(docTitle, 7)
           PostFileName = docpath & docTitle & Letter & ".M"
        End If
        If cwMach.Name = "Ecoca MT-208" Then
           Call CWMachine.SetController("FANUC0IT.ctl", "C:\CAMWorksData\Posts\FANUC0IT.ctl")
           docpath = "\\PRBDC01\DESSINS\CNC\NETWORK\Ecoca" & "\"
           PostFileName = docpath & docTitle & Letter & ".L"
        End If
        setup.PostProcess PostFileName
        If nNumSetups > 1 Then
           Letter = Chr(Asc(Letter) + 1)
        End If
postskip:
    Next j
    swModel.Save3 1, nErrors, nWarnings
    Set cwApp = Nothing
    Set swApp = Nothing
test:
End Sub

VP
VP 2015-11-13

Thank you very much, you're really helpful!

CC
CC 2015-11-13

you can do it your own. Just copy your corrupted macro (something to do with version mismatch) to a new one and open it

Please try and let me know

VP
VP 2015-11-13

Thank you very much guys! I was able to finish my macro ! I pasted all you posted in my backup... Just another 2015 bug I think!