37 Replies Latest reply on Jun 28, 2017 2:58 PM by Deepak Gupta

    macro works great 500 times, then crashes

    Terry Raymond

      I have a VB.net macro that finds SW files on my network, opens them one at a time, runs another macro (VBA) on each file, and closes the file.

       

      It runs correctly on ~400-600 files without problem, but then consistently crashes after running for 15-20 minutes. 

       

      Most common error:

      A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in run macro on multiple files.DLL

      **** System.Runtime.InteropServices.COMException (0x80010100): System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED))

         at SolidWorks.Interop.sldworks.ISldWorks.OpenDoc6(String FileName, Int32 Type, Int32 Options, String Configuration, Int32& Errors, Int32& Warnings)

         at run_macro_on_multiple_files.vbproj.SolidWorksMacro.OpenFileAndRunMacro(String filename) in L:\zMacros\run macro on multiple files\SwMacro\SolidWorksMacro.vb:line 153*****

       

      Sometimes it gives this error:

      The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

       

      annoying error2.png

      annoying error.png

       

      The OpenDoc6 seems to work (errors and warnings = 0), but swDoc goes bad.  Bad pointer?

      Solidworks always has the file open when it crashes, so Solidworks does what it is told to do, but doesn't seem to communicate that back to the macro.

      I'm able to manually run the second macro without issue.

      It doesn't crash on the same file twice, seems like it crashes on whatever file it gets to after 15-20 minutes.

      After a crash, I don't need to restart solidworks for it to work again.

       

      I've gone over all the forum posts and couldn't find a solution.  I tried using try-catch; the macro didn't stop but once the error started, the macro encountered the same exception for successive files and couldn't open them.  SPR 524040 looks similar, but is supposidly fixed.

       

      Any ideas?

       

      My code:

       

       

      ' ******************************************************************************

      ' Macro runner  v0.1

      ' Function:  Runs a macro on all files inside a folder and subfolder.

      ' Usage:  Set options below and run.

      ' ******************************************************************************

       

      Imports SolidWorks.Interop.sldworks

      Imports SolidWorks.Interop.swconst

      Imports System

      Imports System.Diagnostics

       

      Partial Class SolidWorksMacro

       

          '*** User Options (Set all these as necessary, back up your data!)***

          Public startpath As String = "S:\QUOTES"  'Root folder Name. Change to folder that has SolidWorks files (end with \)

          Public fileextension1 As String = "sldprt"   'file type to run macro (ie "sldprt")

          Public fileextension2 As String = "sldasm"   'file type to run macro (ie "sldasm")

          Public MacroToRun As String = "L:\zMacros\property copier.swp"        'full path to macro file you want to run on each file

          Public ModuleToRun As String = "propertycopier1"       'module inside the macro you want to run

       

          Public FileLimit As Integer = 50000    'max number of files to run macro on

          Public FileStart As Integer = 1360        'number of file to start processing

          Public FolderLimit As Integer = 50000  'max number of folders to check

          Public DepthLimit As Integer = 100     'max depth of subfolders to check (0 = don't look in any subfolders)

          '*** end options ***

       

       

          Public swApp As SldWorks

          Public swFiletype As Integer

          Public FolderCount As Integer = 0

          Public FileCount As Integer = 0

          Public FolderDepth As Integer = 0

       

          Public Sub main()

       

              Debug.Print("  === Start === ")

       

              'Start looking for files

              FindFilesInSubfolders(startpath)

       

          End Sub

       

          'FindFilesInSubfolders

          'looks for all files with the right file extension in a folder

          'runs OpenFileAndRunMacro on each found file

          'looks for subdirectories in the folder, and recursively runs itself

          'will stop when it hits either the Folder limit, File limit, or folder depth limit.

       

          Sub FindFilesInSubfolders(ByVal path As String)

              Debug.Print("Folder # " & FolderCount & " - " & path & "     depth:" & FolderDepth)

       

              'get a list of all the files (not folders) in current directory

              Dim files1() As System.IO.FileInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetFiles("*." & fileextension1)

              Dim files2() As System.IO.FileInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetFiles("*." & fileextension2)

              Dim AllFiles(0) As System.IO.FileInfo

              Dim skipfile As Boolean = False

              Dim i As Integer = 0

       

              Array.Resize(AllFiles, files1.Length + files2.Length)

       

              For Each file As System.IO.FileInfo In files1

                  AllFiles(i) = file

                  i += 1

              Next

       

              For Each file As System.IO.FileInfo In files2

                  AllFiles(i) = file

                  i += 1

              Next

       

              Debug.Print(" number of files = " & AllFiles.Length)

       

              'loop through each file, and run the external macro function on each file

              For Each file As System.IO.FileInfo In AllFiles

                  Debug.Print("  File # " & FileCount & " - " & file.FullName)

       

       

                  'count how many files we have opened, exit if we have hit the limit

                  If FileCount >= FileLimit + FileStart Then Exit For

       

                  If (file.Exists) Then

                      FileCount += 1

                      skipfile = False

       

                      'see if file is read only

                      If file.Attributes And IO.FileAttributes.ReadOnly Then

                          Debug.Print("      File is read only! removing read only flag.")

                          file.Attributes = file.Attributes - IO.FileAttributes.ReadOnly

                      End If

       

                      'see if file is hidden

                      If file.Attributes And IO.FileAttributes.Hidden Then

                          Debug.Print("      File is hidden, skipping.")

                          skipfile = True

                      End If

       

                      'see if file is temp sw file  (make this try to delete??? or rename??? if fail, file is open by someone.)

                      If Left(file.FullName, 1) = "~" Then

                          Debug.Print("      File is a temporary SW working file, skipping.")

                          skipfile = True

                      End If

       

                      'don't run we've reached the first start file

                      If FileCount <= FileStart Then

                          skipfile = True

                      End If

       

                      'open file unless skipping

                      If (Not skipfile) Then

                          OpenFileAndRunMacro(file.FullName)

                      End If

                  End If

              Next

       

              'get a list of all the subfolders in current directory

              Dim subfolders() As System.IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetDirectories()

              Debug.Print(" number of subfolders = " & subfolders.Length)

       

              'check how deep we are, exit if we have hit the limit

              If FolderDepth > DepthLimit Then Exit Sub

       

              'get ready to go one level deeper

              FolderDepth += 1

       

              'loop through each folder, and recursively run this function on each found folder

              For Each subfolder As System.IO.DirectoryInfo In subfolders

       

                  'count how many folders we have opened, exit if we have hit the limit

                  FolderCount += 1

                  If FolderCount > FolderLimit Then Exit Sub

       

                  'recursively run the search function again on subfolder

                  FindFilesInSubfolders(subfolder.FullName)

              Next

       

              'we are now back a level

              FolderDepth -= 1

       

          End Sub

       

          'OpenFileAndRunMacro

          'Opens a file, runs a macro, saves file, closes file.

       

          Sub OpenFileAndRunMacro(ByVal filename As String)

              Dim swDoc As ModelDoc2 = Nothing

              Dim bool As Boolean

              Dim errors As Long

              Dim warnings As Long

       

              'Exit Sub   'enable to just find and count files

       

              'Open file

              swDoc = swApp.OpenDoc6(filename, GetSWDocType(filename), 0, "", errors, warnings)

              Debug.Print("      Opening file. [" & My.Computer.Clock.LocalTime.ToLongTimeString & "]" & swDoc.GetTitle)

       

              'Run macro

              bool = swApp.RunMacro2(MacroToRun, ModuleToRun, "main", 0, errors)

       

              'Close file

              swApp.CloseDoc(swDoc.GetTitle)

       

          End Sub

       

          Function GetSWDocType(ByVal filename As String) As Long

              ' Determine type of SolidWorks file based on file extension

              If InStr(LCase(filename), "sldprt") > 0 Then

                  GetSWDocType = swDocumentTypes_e.swDocPART

              ElseIf InStr(LCase(filename), "sldasm") > 0 Then

                  GetSWDocType = swDocumentTypes_e.swDocASSEMBLY

              ElseIf InStr(LCase(filename), "slddrw") > 0 Then

                  GetSWDocType = swDocumentTypes_e.swDocDRAWING

              Else           ' Not a SolidWorks file

                  GetSWDocType = swDocumentTypes_e.swDocNONE

              End If

          End Function

      End Class

       

        • Re: macro works great 500 times, then crashes
          Jesse Seger

          I think it might be a version error with the document.  It might be older than 2008 or something.

          • Re: macro works great 500 times, then crashes
            Wayne Matus

            I have seen issues before where opening too many files in one session of SolidWorks will crash after a period of time.

             

            What I did was put a counter in the code so that after x number of files processed, it would close SolidWorks and start it back up and continue processing files.

             

            This would require you to convert your macro into a VB.NET external program, but that should not be too much work since your macro is already a VB.NET macro.

            • Re: macro works great 500 times, then crashes
              Rajat Jain

              Terry,

               

              I faced a similar issue once and I believe it is the same problem. Please goto TaskManager->View->Select Columns and select GDI Objects.

               

              Check the GDI object count for SW when SW crashes. It must have reached its limit. If that is the case then you can simply increase the GDI count limit in Windows Registry.

               

              Hope it helps!!!

               

              Rajat Jain

                • Re: macro works great 500 times, then crashes
                  Terry Raymond

                  Hey good thought.  I watched the GDI count for SW, it varies between 1300 and 1600 during a macro run.  Last time it crashed, it was only at 1484.

                    • Re: macro works great 500 times, then crashes
                      Rajat Jain

                      Terry,

                       

                      Just another weird thought!!

                       

                      I just studied your code. I think it might also be caused if your SW session is closed accidently. If this is done then all your objects shall be disconnected.

                       

                      Sldworks::closeDoc() can cause this behavior if you try to close the last open document and userControl is set to true. Just try to change UserControl (=false) and see if it changes the behavior.

                       

                      Hope it helps!!!

                       

                      Rajat Jain

                        • Re: macro works great 500 times, then crashes
                          Terry Raymond

                          Thanks for ideas.  I had tried that along with

                          swApp.UserControl = False

                          swApp.Visible = False

                          swApp.Frame.KeepInvisible = True

                           

                          It made it go faster! but still crash around 500 files.

                           

                          Also have tried using OpenDoc instead of OpenDoc6, and CloseAllDocuments instead of CloseDoc.

                           

                          Error still occured on file #503, was "Unable to cast COM object of type 'System.__ComObject' to interface type 'SolidWorks.Interop.sldworks.ModelDoc2'. This operation failed because the QueryInterface call on the COM component for the interface with IID"

                           

                           

                           

                          Wondering if swApp is getting redefined somewhere.. is it possible to reassign it to the Solidworks program?

                           

                          I contacted my VAR who said to get with SW API help.  Lets see what they say...

                            • Re: macro works great 500 times, then crashes
                              Wayne Matus

                              One way to check is to test if swApp Is Nothing. Also check for any errors or warnings in your OpenDoc6 line of code along with swDoc being Nothing.

                               

                              Another thing to try is setting the Options parameter of the OpenDoc6 method to swOpenDocOptions_Silent.

                                • Re: macro works great 500 times, then crashes
                                  Terry Raymond

                                  No luck.  =(

                                   

                                  Here is a simple macro that creates the same problem after opening and closing the same file ~500 times.

                                   

                                  Dim errors As Long
                                  Dim warnings As Long
                                  Dim filename As String = "C:\test\test.sldprt"
                                  Dim i As Integer = 0
                                  
                                  swApp.UserControl = False
                                  swApp.Visible = False
                                  swApp.Frame.KeepInvisible = True
                                  
                                  For i = 1 To 450
                                      Debug.Print(i)
                                      swDoc = swApp.OpenDoc6(filename, GetSWDocType(filename), 0, "", errors, warnings)
                                      swApp.CloseAllDocuments(True)
                                  Next i
                                  
                                  swApp.UserControl = True
                                  swApp.Visible = True
                                  swApp.Frame.KeepInvisible = False
                                  
                                  For i = i To 1000
                                      Debug.Print(i)
                                      swDoc = swApp.OpenDoc6(filename, GetSWDocType(filename), 0, "", errors, warnings)
                                      swApp.CloseAllDocuments(True)
                                  Next i
                                  
                                    • Re: macro works great 500 times, then crashes
                                      Wayne Matus

                                      I ran your simple macro and it finished both loops without crashing. I ran on x64 system with 8GB of ram. I did not check memory usage.

                                        • Re: macro works great 500 times, then crashes
                                          Terry Raymond

                                          wow... interesting.  Thanks for letting me know.  My comp has same specs, but running SW 2010SP5.

                                           

                                          I just got my simple macro to work in VBA without crashing.  I'm going to try rewriting my VB.net code to vba.

                                            • Re: macro works great 500 times, then crashes
                                              Wayne Matus

                                              I ran it again and at the end it was using 560MB of memory. It was a simple part, circle extruded to make cylindrical solid body.

                                               

                                              Other option is to download free Xpress version of VB.NET and convert your macro to an .exe file.

                                                • Re: macro works great 500 times, then crashes
                                                  Terry Raymond

                                                  Ok I ran a vba version of my macro last night.  SW crashed after ~3300 files, probably hit a memory leak limit.

                                                   

                                                  Your suggestion sounds like a good idea, I'll try that next.

                                                   

                                                  It is simply amazing how many walls I am hitting...!

                                                  • Re: macro works great 500 times, then crashes
                                                    Terry Raymond

                                                    Ok I need a little bit more help here...

                                                     

                                                    I did everything here:

                                                    http://help.solidworks.com/2011/English/api/sldworksapiprogguide/GettingStarted/Visual_Basic_.NET_Standalone_and_Add-in_Applications.htm?id=3f324a3bd1ea4a9b8914745b7832d357#Pg0

                                                     

                                                    I added these  References

                                                    SldWorks 2010 Type Library

                                                    Solidworks 2010 Constant type library

                                                     

                                                    I had to comment out "Imports SolidWorks.Interop.sldworks" because vb express doesn't recognize it.  It recognizes the other solidworks functions and things.

                                                     

                                                    It runs, but hangs on a "Preparing Solidworks for first use" dialog... help?

                                                      • Re: macro works great 500 times, then crashes
                                                        Wayne Matus

                                                        Just

                                                        imports sldworks

                                                        imports swconst

                                                        • Re: macro works great 500 times, then crashes
                                                          Wayne Matus

                                                          Here is your macro exported to VB.NET that should work for you

                                                           

                                                          Imports SldWorks

                                                          Imports SwConst

                                                          Imports System

                                                          Imports System.Diagnostics

                                                          Public Class Form1

                                                              '*** User Options (Set all these as necessary, back up your data!)***

                                                              Public startpath As String = "c:\WORK"  'Root folder Name. Change to folder that has SolidWorks files (end with \)

                                                              Public fileextension1 As String = "sldprt"   'file type to run macro (ie "sldprt")

                                                              Public fileextension2 As String = "sldasm"   'file type to run macro (ie "sldasm")

                                                              Public MacroToRun As String = "L:\zMacros\property copier.swp"        'full path to macro file you want to run on each file

                                                              Public ModuleToRun As String = "propertycopier1"       'module inside the macro you want to run

                                                           

                                                              Public FileLimit As Integer = 50    'max number of files to run macro on

                                                              Public FileStart As Integer = 1        'number of file to start processing

                                                              Public FolderLimit As Integer = 50000  'max number of folders to check

                                                              Public DepthLimit As Integer = 100     'max depth of subfolders to check (0 = don't look in any subfolders)

                                                              '*** end options ***

                                                           

                                                              Public swApp As SldWorks.SldWorks

                                                              Public swFiletype As Integer

                                                              Public FolderCount As Integer = 0

                                                              Public FileCount As Integer = 0

                                                              Public FolderDepth As Integer = 0

                                                              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                                                                  Debug.Print("  === Start === ")

                                                                  ' Connect to running instance of SolidWorks

                                                                  Try

                                                                      swApp = GetObject(, "SldWorks.Application")

                                                                  Catch ex As Exception

                                                                      MsgBox(ex)

                                                                  End Try

                                                           

                                                                  'Start looking for files

                                                                  FindFilesInSubfolders(startpath)

                                                              End Sub

                                                           

                                                              'FindFilesInSubfolders

                                                              'looks for all files with the right file extension in a folder

                                                              'runs OpenFileAndRunMacro on each found file

                                                              'looks for subdirectories in the folder, and recursively runs itself

                                                              'will stop when it hits either the Folder limit, File limit, or folder depth limit.

                                                           

                                                              Sub FindFilesInSubfolders(ByVal path As String)

                                                                  Debug.Print("Folder # " & FolderCount & " - " & path & "     depth:" & FolderDepth)

                                                           

                                                                  'get a list of all the files (not folders) in current directory

                                                                  Dim files1() As System.IO.FileInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetFiles("*." & fileextension1)

                                                                  Dim files2() As System.IO.FileInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetFiles("*." & fileextension2)

                                                                  Dim AllFiles(0) As System.IO.FileInfo

                                                                  Dim skipfile As Boolean = False

                                                                  Dim i As Integer = 0

                                                           

                                                                  Array.Resize(AllFiles, files1.Length + files2.Length)

                                                           

                                                                  For Each file As System.IO.FileInfo In files1

                                                                      AllFiles(i) = file

                                                                      i += 1

                                                                  Next

                                                           

                                                                  For Each file As System.IO.FileInfo In files2

                                                                      AllFiles(i) = file

                                                                      i += 1

                                                                  Next

                                                           

                                                                  Debug.Print(" number of files = " & AllFiles.Length)

                                                           

                                                                  'loop through each file, and run the external macro function on each file

                                                                  For Each file As System.IO.FileInfo In AllFiles

                                                                      Debug.Print("  File # " & FileCount & " - " & file.FullName)

                                                           

                                                                      'count how many files we have opened, exit if we have hit the limit

                                                                      'If FileCount >= FileLimit + FileStart Then Exit For

                                                           

                                                                      If (file.Exists) Then

                                                                          FileCount += 1

                                                                          skipfile = False

                                                           

                                                                          'see if file is read only

                                                                          If file.Attributes And IO.FileAttributes.ReadOnly Then

                                                                              Debug.Print("      File is read only! removing read only flag.")

                                                                              file.Attributes = file.Attributes - IO.FileAttributes.ReadOnly

                                                                          End If

                                                           

                                                                          'see if file is hidden

                                                           

                                                                          If file.Attributes And IO.FileAttributes.Hidden Then

                                                                              Debug.Print("      File is hidden, skipping.")

                                                                              skipfile = True

                                                                          End If

                                                           

                                                                          'see if file is temp sw file  (make this try to delete??? or rename??? if fail, file is open by someone.)

                                                                          If Microsoft.VisualBasic.Left(file.FullName, 1) = "~" Then

                                                                              Debug.Print("      File is a temporary SW working file, skipping.")

                                                                              skipfile = True

                                                                          End If

                                                           

                                                                          'don't run we've reached the first start file

                                                                          If FileCount <= FileStart Then

                                                                              skipfile = True

                                                                          End If

                                                           

                                                                          'open file unless skipping

                                                                          If (Not skipfile) Then

                                                                              If FileCount >= FileLimit + FileStart Then

                                                                                  ' Kill SolidWorks and restart it

                                                                                  swApp.ExitApp()

                                                                                  ' Start new instance of SolidWorks

                                                                                  swApp = New SldWorks.SldWorks

                                                                                  swApp.UserControl = True

                                                                                  swApp.Visible = True

                                                                                  ' reset file counter

                                                                                  FileCount = 0

                                                                              End If

                                                                              OpenFileAndRunMacro(file.FullName)

                                                                          End If

                                                                      End If

                                                                  Next

                                                                  'get a list of all the subfolders in current directory

                                                                  Dim subfolders() As System.IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(path).GetDirectories()

                                                                  Debug.Print(" number of subfolders = " & subfolders.Length)

                                                           

                                                                  'check how deep we are, exit if we have hit the limit

                                                                  If FolderDepth > DepthLimit Then Exit Sub

                                                                  'get ready to go one level deeper

                                                                  FolderDepth += 1

                                                           

                                                                  'loop through each folder, and recursively run this function on each found folder

                                                                  For Each subfolder As System.IO.DirectoryInfo In subfolders

                                                                      'count how many folders we have opened, exit if we have hit the limit

                                                                      FolderCount += 1

                                                                      If FolderCount > FolderLimit Then Exit Sub

                                                           

                                                           

                                                           

                                                                      'recursively run the search function again on subfolder

                                                           

                                                                      FindFilesInSubfolders(subfolder.FullName)

                                                           

                                                                  Next

                                                           

                                                           

                                                           

                                                                  'we are now back a level

                                                           

                                                                  FolderDepth -= 1

                                                           

                                                           

                                                           

                                                              End Sub

                                                           

                                                           

                                                           

                                                              'OpenFileAndRunMacro

                                                           

                                                              'Opens a file, runs a macro, saves file, closes file.

                                                           

                                                           

                                                           

                                                              Sub OpenFileAndRunMacro(ByVal filename As String)

                                                           

                                                                  Dim swDoc As ModelDoc2 = Nothing

                                                           

                                                                  Dim bool As Boolean

                                                           

                                                                  Dim errors As Long

                                                           

                                                                  Dim warnings As Long

                                                           

                                                           

                                                           

                                                                  'Exit Sub   'enable to just find and count files

                                                           

                                                           

                                                           

                                                                  'Open file

                                                           

                                                                  swDoc = swApp.OpenDoc6(filename, GetSWDocType(filename), 0, "", errors, warnings)

                                                           

                                                                  Debug.Print("      Opening file. [" & My.Computer.Clock.LocalTime.ToLongTimeString & "]" & swDoc.GetTitle)

                                                           

                                                           

                                                           

                                                                  'Run macro

                                                           

                                                                  'bool = swApp.RunMacro2(MacroToRun, ModuleToRun, "main", 0, errors)

                                                           

                                                           

                                                           

                                                                  'Close file

                                                           

                                                                  swApp.CloseDoc(swDoc.GetTitle)

                                                           

                                                           

                                                           

                                                              End Sub

                                                           

                                                           

                                                           

                                                              Function GetSWDocType(ByVal filename As String) As Long

                                                           

                                                                  ' Determine type of SolidWorks file based on file extension

                                                           

                                                                  If InStr(LCase(filename), "sldprt") > 0 Then

                                                           

                                                                      GetSWDocType = swDocumentTypes_e.swDocPART

                                                           

                                                                  ElseIf InStr(LCase(filename), "sldasm") > 0 Then

                                                           

                                                                      GetSWDocType = swDocumentTypes_e.swDocASSEMBLY

                                                           

                                                                  ElseIf InStr(LCase(filename), "slddrw") > 0 Then

                                                           

                                                                      GetSWDocType = swDocumentTypes_e.swDocDRAWING

                                                           

                                                                  Else           ' Not a SolidWorks file

                                                           

                                                                      GetSWDocType = swDocumentTypes_e.swDocNONE

                                                           

                                                                  End If

                                                           

                                                              End Function

                                                           

                                                          End Class

                                                            • Re: macro works great 500 times, then crashes
                                                              Terry Raymond

                                                              Holy crap!  Do I owe you money...?

                                                               

                                                              Will take a few hours to check, apparently I need to reinstall .Net and let it compile for a few hours. =(

                                                              • Re: macro works great 500 times, then crashes
                                                                Terry Raymond

                                                                Ok status update

                                                                Running 1 session of solidworks went for 3300 files before crash.

                                                                 

                                                                Closing / opening solidworks is finally working -

                                                                I had to do everything in S-050248 a few times and leave my computer on overnight to get rid of the "Preparing Solidworks for first use" dialog. 

                                                                 

                                                                I am noticing that

                                                                swApp.ExitApp()

                                                                closes the windo, but doesn't kill the process, and when It runs the next time, the memory usage stays the same.  So I think I am going to hit the same 3300 file limit.   Looking into this now...

                                                                 

                                                                Also noticed that launching solidworks, not opening any files, closing it, then launching it again causes a crash.  Macro sometimes (repeatably) will crash after opening a specific file last before close.  But so long as that file isn't the last file before close, it will keep working.  wierd.

                                                                 

                                                                Thanks so much for your help so far.  I have an imperfect but workable solution now.

                                                                  • Re: macro works great 500 times, then crashes
                                                                    Wayne Matus

                                                                    I have seen that same issue of swApp.ExitApp not killing the process so I have added something like this to code when it happened.

                                                                     

                                                                        Dim p As Process
                                                                        For Each p In System.Diagnostics.Process.GetProcessesByName("SldWorks")
                                                                            p.Kill()
                                                                        Next
                                                                    • Re: macro works great 500 times, then crashes
                                                                      Terry Raymond

                                                                      Ok this seems to work:

                                                                       

                                                                      'beat SolidWorks with a stick until it closes

                                                                      swApp.ExitApp()

                                                                      Marshal.ReleaseComObject(swApp)

                                                                      swApp = Nothing

                                                                      Thread.Sleep(5000)

                                                                       

                                                                      edit - Whoops, didn't see your reply until after posting.

                                                                       

                                                                      Anyways, it looks like this sucker is finally working.  I'll run it over the weekend and see how it does.  Thanks for your help wayne!

                                                                        • Re: macro works great 500 times, then crashes
                                                                          Terry Raymond

                                                                          Ok I got it to work!  Seems like all sorts of things kept crashing solidworks and throwing exceptions at the macro.  So I made this function to close out solidworks and start a new one whenever something bad happened.

                                                                           

                                                                          Sub LaunchNewSolidworks()
                                                                          If Not (swApp Is Nothing) Then
                                                                          
                                                                          'close SolidWorks and beat it with a stick
                                                                          Try
                                                                          swApp.ExitApp()
                                                                          Marshal.ReleaseComObject(swApp)
                                                                          swApp = Nothing
                                                                          Thread.Sleep(5000)
                                                                          Catch ex As Exception
                                                                          Debug.Print(ex.ToString)
                                                                          End Try
                                                                          
                                                                          End If
                                                                          
                                                                          'really make sure there is no sw running
                                                                          Dim p As Process
                                                                          For Each p In System.Diagnostics.Process.GetProcessesByName("SldWorks")
                                                                          p.Kill()
                                                                          Next
                                                                          Thread.Sleep(5000)
                                                                          
                                                                          'Start new instance of SolidWorks
                                                                          swApp = CreateObject("SldWorks.Application")
                                                                          'swApp = New SldWorks.SldWorks
                                                                          
                                                                          'Thread.Sleep(2000)
                                                                          
                                                                          swApp.UserControl = False
                                                                          swApp.Visible = False
                                                                          End Sub
                                                                          

                                                                           

                                                                          Ran on 40,000 files during the weekend.

                                                                           

                                                                          Thanks again for all the assistance.

                                                                           

                                                                          I will make a user friendly vb application in the future.  How much do the VAR's charge for theirs?

                                                • Re: macro works great 500 times, then crashes
                                                  Terry Raymond

                                                  I made this script into a useful little utility.  Thanks much to Wayne for getting me over the learning curve hump.

                                                  http://www.t-monster.com/SW%20Macro%20Runner%20installer.zip

                                                   

                                                  Clipboard01.png