9 Replies Latest reply on May 16, 2017 5:51 PM by Thomas Walder

    GetPrintSpecification causes crashes during print macro

    Thomas Walder

      Hello All,

       

      I am having trouble with a macro causing Solidworks to crash.  What I'm trying to do is create an auto-print macro that can be bound to a shortcut that will automatically set print spec/page setup information based on the sheet size.  I'm able to run the macro and it works as it should ~90% of the time but  sometimes I will get a prompt indicating that the macro had a "Serious error" and that the system may now be unstable.  Solidworks usually crashes shortly after this prompt.

       

      The problem seems to happen randomly and does not occur with any specific drawings or drawings that have certain elements (design tables, configs, cut out views, etc...). If I step through the code the macro crashes on the line "Set swPrintSpec = swDocExt.GetPrintSpecification" 

       

      My workflow to get the crash is as follows:

      • Open Drawing
      • Run Macro (note: if the macro runs successfully once, I can re-run many times and it does not crash the program provided I do not close the drawing or change focus)
      • Close Drawing
      • Open Drawing
      • Run Macro
      • Close Drawing
      • .... repeat maybe 10-15 times
      • Open Drawings
      • Run Macro
      • SERIOUS ERROR PROMPT

       

      I have had the error occur by closing/opening the same drawing many times or by opening different drawings.  Here is a few things I have noticed that may be of help.

      • It has failed both by just opening a drawing and running the macro and also by opening a drawing clicking to ensure focus and then running the macro
      • When drawing is closed (nothing open) or the wrong document type is opening I get different errors.
        • Wrong Type (sldasm,sldprt...etc) I get run time error '13'  "type mismatch" on "Set swDrawing = swApp.ActiveDoc"
        • Nothing open, I get a run time error '91' "Object variable or With block variable not set" on "Set swDocExt = swDoc.Extension"
        • Neither of these two errors cause the SERIOUS ERROR prompt.  I can debug or end the macro and everything runs normally afterwards.
      • I can comment the PrintOut line and still get the error, useful for testing the macro while saving paper.

       

      I have done a lot of reading to try and get around this problem with no luck.  As far as I know the only way to get the PrintSpec object is using "GetPrintSpecification".  I have tried just creating a blank print spec and manually populating the properties but I get an "ActiveX cannot create object error".

       

      One other thing to note is that I also wrote a very similar macro in an excel spread sheet which will do batch printing.  The only real difference between the two macros is that the excel macro opens and closes the drawings through VBA based on what is put in the spreadsheet as opposed to using the "swApp.ActiveDoc".  The printing portion is basically the same, I just have not cleaned up the code yet.  I have yet to get the "SERIOUS ERROR" prompt in the batch printing macro.

       

      I have attached both macros for reference.

           AutoPrint.swp - Prints the currently active drawing to the default printer.  (I have paper size/source hardcoded in both macros, I'm not sure if this is computer specific)

           List Batch Printer.xlsm - Batch prints a list of files obtained from the "File References" of an assembly document and pasting them into cell A1.  Valid print paths are set in the VBA code with sPrintPath at the top of  the "main()" procedure.  The following assumptions are made:

      • Drawing name is same as Part/Assembly
      • Drawing is located in the same folder as Part/Asembly

       

      I would appreciate any help you guys are able to offer.

        • Re: GetPrintSpecification causes crashes during print macro
          Christian Chu

          I haven't see your code yet but have seen something similar like this before

          Try this:

          Add a "Warning" mesg at the beginning of the sub to tell the users not to hit any key until the job is done and a "DONE" mesg at the end of the sub to let the users know it's done

          What happen is Excel is the third party and running in the back without the users notice. Hitting any key might interupt the process and it's very good chance to crash  SW- so the mesg should be a reminder to prevent this happens

            • Re: GetPrintSpecification causes crashes during print macro
              Thomas Walder

              Just to clarify, the excel macro (batch printing) works fine.  At least so far it has, I have not had a serious error prompt yet.  I get the error when trying to do a "quick print" macro to print the current document.  I have the macro assigned to a shortcut (ctrl-shift-p).  When the error occurs it is basically immediately after I start the macro.

               

                   Option Explicit

               

                   'Declare global parameters  

                   Dim swApp As SldWorks.SldWorks

                   Dim swDoc As SldWorks.ModelDoc2

                   Dim swDrawing As SldWorks.DrawingDoc

                   Dim swDocExt As SldWorks.ModelDocExtension

                   Dim swSheet As SldWorks.Sheet

                   Dim swPageSetup As SldWorks.PageSetup

                   Dim swPrintSpec As SldWorks.PrintSpecification

                   Dim vSheetProperties As Variant

                   Dim sDrawing As String

               

                   Sub main()

               

                   'Set objects to current session and current document open in Solidworks    

                   Set swApp = Application.SldWorks

                   Set swDoc = swApp.ActiveDoc

                   Set swDocExt = swDoc.Extension

                   Set swDrawing = swDoc

                   Set swSheet = swDrawing.GetCurrentSheet

               

                   'Set Page Setup & Print Spec

                   swDocExt.UsePageSetup = swPageSetupInUse_e.swPageSetupInUse_Document

                   Set swPageSetup = swDoc.PageSetup

                   Set swPrintSpec = swDocExt.GetPrintSpecification '**************** This line occasionally cause system instability**************

               

                   'Set print range... need to add 1 more to add print range to get correct amount of sheets

                   swPrintSpec.ResetPrintRange

                   swPrintSpec.AddPrintRange 1, swDrawing.GetSheetCount + 1

               

              This is the top of the macro.  Do you see anything that could cause the crashing?

            • Re: GetPrintSpecification causes crashes during print macro
              Peter Kennedy

              I had very similar issues when I tried to use GetPrintSpecification as well. GetPrintSpecification causing Solidworks to Crash

               

              I dealt with my VAR with the issue as well and they weren't able to provide any support regarding the issue. Maybe to contact APIsupport@solidworks.com and see if they'll help you out.

                • Re: GetPrintSpecification causes crashes during print macro
                  Thomas Walder

                  Your thread was one of the things I came across when trying to research the issue.  Were you ever able to get it to run without crashing?  I know in your case there was a different way to get the sheet count, so I assume you didn't do any more testing on it.  I did try to recreate your issue but I was not able to get Solidworks to crash the same way.

                   

                  I have reached out to  APIsupport@solidworks.com and they are looking at it as well.  I just wanted to work on the problem from two fronts, also the response time is a bit faster when using the forums.

                    • Re: GetPrintSpecification causes crashes during print macro
                      Peter Kennedy

                      I did, however I had to use Save as PDF instead of print. I set the sheet I wanted to save using this:

                       

                          'Specifies the PDF data

                          boolstatus = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, strSheetName)

                       

                      I gave up trying to print, nothing I did would have it print the sheets I needed to continuously with out crashing. Are you actually print drawing or just trying to create a PDF? The save as a PDF I find doesn't produce as good of drawings as print does but it works for what I need.

                        • Re: GetPrintSpecification causes crashes during print macro
                          Thomas Walder

                          I'm actually trying to physically print a drawing.  It works most of the time but the crashes are frequent enough that I don't really want to use the macro.

                           

                          The weird thing is that my other macro for batch printing seems to work just fine.  It hasn't crashed at all since I've been testing it and I've probably printed 100 drawings or so with it.  With the "autoprint" macro I can barely get 10-15 drawings with out crashing Solidworks.

                            • Re: GetPrintSpecification causes crashes during print macro
                              Peter Kennedy

                              The one thing my VAR suggested which didn't help me but might help you was adding in the restore and reset after you have finished printing.

                               

                                  printSpec.RestoreDefaults

                                  printSpec.ResetPrintRange

                               

                              That is from this example: 2015 SOLIDWORKS API Help - Print Drawing Example (VBA)

                               

                              And I know they're all supersede by printout4 but have you tried printout3 to see if that might eliminate that error message? That way you aren't dealing with print specification as I feel like there is an error in that code somewhere.

                                • Re: GetPrintSpecification causes crashes during print macro
                                  Thomas Walder

                                  I can try it but I'm not sure it would help.  When it crashes it doesn't even get past this line, which is before all the printing takes place:

                                   

                                       Set swPrintSpec = swDocExt.GetPrintSpecification

                                   

                                  Once it hits that line it produces the "Serious error prompt" and I don't know why, it happens maybe 10% of the time.  If it does make it past that line it prints no problems.

                                   

                                  EDIT: I see now what you are saying about printout3.  I will try that and see if using that method works better.

                                  • Re: GetPrintSpecification causes crashes during print macro
                                    Thomas Walder

                                    I didn't try PrintOut3 since it is obsolete, but I did try PrintDirect and that seems to work.  I'm going to modify my macros to use PrintDirect instead of PrintOut4, like you said, I also think there is something wrong with the PrintSpecification object.  Here's the latest code that I have.

                                     

                                    Option Explicit

                                     

                                    'Declare global parameters

                                    Dim swApp As SldWorks.SldWorks

                                    Dim swDoc As SldWorks.ModelDoc2

                                    Dim swDrawing As SldWorks.DrawingDoc

                                    Dim swDocExt As SldWorks.ModelDocExtension

                                    Dim swSheet As SldWorks.Sheet

                                    Dim swPageSetup As SldWorks.PageSetup

                                    Dim vSheetProperties As Variant

                                    Dim sDrawing As String

                                     

                                    Sub main()

                                     

                                    'Set objects to current session and current document open in Solidworks

                                    Set swApp = Application.SldWorks

                                    Set swDoc = swApp.ActiveDoc

                                    Set swDocExt = swDoc.Extension

                                    Set swDrawing = swDoc

                                    Set swSheet = swDrawing.GetCurrentSheet

                                     

                                    'Set Page Setup & Print Spec

                                    swDocExt.UsePageSetup = swPageSetupInUse_e.swPageSetupInUse_Document

                                    Set swPageSetup = swDoc.PageSetup

                                     

                                    'Get the current sheets properties

                                    vSheetProperties = swSheet.GetProperties2

                                    Dim z As Integer

                                    Debug.Print "Printing Drawing: " & sDrawing

                                    For z = 0 To 7

                                        Debug.Print "Sheet Properties " & z & ": " & vSheetProperties(z)

                                    Next z

                                     

                                    'Set paper size based on width/length properties of sheet and whether or not the drawing is landscape/portrait

                                    If vSheetProperties(5) > vSheetProperties(6) Then

                                        swPageSetup.PrinterPaperLength = vSheetProperties(5) * 1000

                                        swPageSetup.PrinterPaperWidth = vSheetProperties(6) * 1000

                                    Else

                                        swPageSetup.PrinterPaperLength = vSheetProperties(6) * 1000

                                        swPageSetup.PrinterPaperWidth = vSheetProperties(5) * 1000

                                    End If

                                     

                                    swPageSetup.ScaleToFit = False

                                    swPageSetup.Scale2 = 100

                                     

                                    'use select case to set printer page setup and printer specs for current sheet size

                                    Select Case vSheetProperties(0)

                                        Case Is = swDwgPaperSizes_e.swDwgPaperAsize

                                            Debug.Print "A Size Landscape Selected"

                                            swPageSetup.PrinterPaperSize = 1                'A size sheet

                                            swPageSetup.PrinterPaperSource = 15             'A size source

                                        Case Is = swDwgPaperSizes_e.swDwgPaperBsize

                                            Debug.Print "B Size Landscape Selected"

                                            swPageSetup.PrinterPaperSize = 17               'B size sheet

                                            swPageSetup.PrinterPaperSource = 7              'B size source

                                        Case Is = swDwgPaperSizes_e.swDwgPaperCsize

                                            Debug.Print "C Size Landscape Selected"

                                            swPageSetup.PrinterPaperSize = 1                'A size sheet

                                            swPageSetup.PrinterPaperSource = 15             'A size source

                                            swPageSetup.Scale2 = 50                         'reduce scale for 1/2 size printing

                                        Case Is = swDwgPaperSizes_e.swDwgPaperDsize

                                            Debug.Print "D Size Landscape Selected"

                                            swPageSetup.PrinterPaperSize = 17               'B size sheet

                                            swPageSetup.PrinterPaperSource = 7              'B size source

                                            swPageSetup.Scale2 = 50                         'reduce scale for 1/2 size printing

                                    End Select

                                     

                                    'Print Drawing to Default Printer

                                    swDoc.PrintDirect

                                     

                                    Debug.Print "*** FILE PROPERTIES ***"

                                    Debug.Print "Title: " & swDoc.GetTitle

                                    Debug.Print "*** PRINT OUTPUT SETTINGS ***"

                                    Debug.Print "--- Page Setup ---"

                                    With swPageSetup

                                        Debug.Print "Center Footer: " & .CenterFooter

                                        Debug.Print "Center Header: " & .CenterHeader

                                        Debug.Print "Drawing Color: " & .DrawingColor

                                    '       Debug.Print "Footer Text Format: " & .FooterTextFormat

                                    '       Debug.Print "Header Text Format: " & .HeaderTextFormat

                                        Debug.Print "High Quality: " & .HighQuality

                                        Debug.Print "Left Footer: " & .LeftFooter

                                        Debug.Print "Left Header: " & .LeftHeader

                                        Debug.Print "Orientation: " & .Orientation

                                        Debug.Print "Printer Paper Length: " & .PrinterPaperLength

                                        Debug.Print "Printer Paper Size: " & .PrinterPaperSize

                                        Debug.Print "Printer Paper Source: " & .PrinterPaperSource

                                        Debug.Print "Printer Paper Width: " & .PrinterPaperWidth

                                        Debug.Print "Right Footer: " & .RightFooter

                                        Debug.Print "Right Header: " & .RightHeader

                                        Debug.Print "Scale2: " & .Scale2

                                        Debug.Print "Scale Draft Edges: " & .ScaleDraftEdges

                                        Debug.Print "Scale to Fit: " & .ScaleToFit

                                        Debug.Print "Use Page Setup On Sheets: " & .UsePageSetupOnSheets

                                    End With

                                     

                                    Debug.Print "--- Sheet Information ---"

                                    With swSheet

                                        Debug.Print "Sheet Format Name: " & .GetSheetFormatName

                                        Debug.Print "Number of Sheets: " & swDrawing.GetSheetCount

                                        'Debug.Print "Size: " & .GetSize

                                    End With

                                     

                                     

                                    End Sub