6 Replies Latest reply on Jun 19, 2015 1:24 PM by Paul Conder

    batch saving of pdf's results in GDI handle depletion

    Paul Conder



      RE: SW version = SW 2015 workgroup pdm

      the macro below runs a batch save of all the drawings in a particular directory and saves in another directory.


      unfortunately, running the macro results in a 'hanging' of SW after some 400 files.

      the reason for that is depletion of the number of GDI handles/objects.

      under normal circumstances, SW is using 1000 to 1500 GDI handles.

      while the macro is running, the number of handles steadily creeps up to approx 9000 after some 400 files; SW becomes sluggish and finally hangs; requiring a restart.


      this depletion of resources has been addressed in a blog:


      under the name:  System resources running low ? (a.k.a. GDI handle depletion)

      according to the blog however, the issue should have been resolved in a SW 2011 update.


      there is of course a second possibility:  a  coding error in the macro, which I haven't been able to detect.


      could someone with similar experiences help me out here ?
      many thanks,




      Option Explicit

      Sub main()

      Dim swApp           As SldWorks.SldWorks

      Dim swModel         As SldWorks.ModelDoc2

      Dim swExportPDFData As SldWorks.ExportPdfData

      Dim boolstatus      As Boolean

      Dim longstatus      As Long

      Dim longwarnings    As Long

      Dim Part            As Object

      Dim sFileName       As String

      Dim sModelName      As String

      Dim sPartName       As String

      Dim nErrors         As Long

      Dim nWarnings       As Long

      Const PathSrc       As String = "Z:\solidworks\drawings\"

      Const PathTgt       As String = "Z:\hotfolder\pdf\"

      Const PathJpg       As String = "Z:\hotfolder\Thumbnails\"


      On Error Resume Next

      Set swApp = Application.SldWorks

      sFileName = Dir(PathSrc & "*.slddrw")

      swExportPDFData.ViewPdfAfterSaving = False

      Do Until sFileName = ""

          Set swModel = swApp.OpenDoc6(PathSrc & sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)

          Set swModel = swApp.ActiveDoc

          sModelName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

          sModelName = PathTgt & Left(sModelName, InStrRev(sModelName, ".") - 1) & ".PDF"

          Set swExportPDFData = swApp.GetExportFileData(1)



          swModel.Extension.SaveAs(sModelName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swExportPDFData, nErrors, nWarnings)

          If Not swModel Is Nothing Then

              swApp.CloseDoc swModel.GetTitle

          End If

          Set swModel = Nothing

          sFileName   = Dir


      End Sub


        • Re: batch saving of pdf's results in GDI handle depletion
          Deepak Gupta

          The codes are OK. You can put SOLIDWORKS to either sleep after doing certain number of drawing, so that it can clear off GDI handles OR do a restart in between for same thing.


          You can also use task scheduler for creating PDFs which can take of the GDI issue.

            • Re: batch saving of pdf's results in GDI handle depletion
              Paul Conder

              thank you very much for your quick reply !

              I have looked into a lot of your posts to get familiar with the API and macros; that was very helpful indeed.


              what we are trying to achieve is to link rather straightforward SW output to a mysql database environment;

              in two separate actions:


              1.  background macro, starting with SW, always active.

              when saving a SW file , the macro saves:

              1a  a pdf

              1b  a jpg

              1c  a text string with some metadata


              2.  button macro saving BOM's


              with respect to 1. I was afraid that it would not be possible, because of the issue mentioned in my initial post.


              if I understand you correctly, SW does clean up the GDI handles/objects on a regular basis.

              which implies that the background macro would not have the same effect (hanging) after a long day with very many saving actions.

              could you confirm this statement ?


              thanks again,


            • Re: batch saving of pdf's results in GDI handle depletion
              Simon Turner

              Just to add that you can increase the avaiable GDI handles in the registry.


              HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota


              can be increased up to 65335. 10,000 is a good first try.