AnsweredAssumed Answered

GetPrintSpecification causes crashes during print macro

Question asked by Thomas Walder on May 16, 2017
Latest reply on May 16, 2017 by 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


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.