11 Replies Latest reply on May 28, 2015 10:55 AM by Matt Martens

    How to print to selected printer and selected tray?

    Hector Molina

      Hi everyone, and thank you for your interest in my question, and any help that you may be able to offer.

       

      I have not done any programming in about 20 years, I just purchased a book on VBA, "Automating Solidworks 2012 Using Macros". And also will start reading other VBA information. My main automation language was LISP.

      My need is to create a macro, that can send the print to the selected printer, and to the selected printer tray. A modification of the same macro, or in the same code, have the ability to select the plotter and set the needed paper options, size, rotation, etc. My thoughts are to create a toolbar with buttons for each specific printing device and paper size.

      I realize that for a novice, this is a very ambition task, but I was hoping that someone may be able to share code with me, or a complete macro that has some of these features coded already in it. I would like to read and learn from the code. What I would like is code to get the device information. I tried to record a macro while setting up the options, but it doesn't seem to record the printer/plotter setttings.

       

      Again, your help is greatly apreciated.

       

      Hector

        • Re: How to print to selected printer and selected tray?
          Matt Martens

          Here is the macro I use to skip past all the steps of printing. The code contains a Select Case statement that analyzes the page size of the current sheet and changes certain variables in the page setup accordingly.

           

          The macro will need to be altered so it will use your printers and page sizes. I got the variables for mine by recording my printing process.

            • Re: How to print to selected printer and selected tray?
              Hector Molina

              Matt,

               

              Thank  you very much for sharing this macro with me. It will work even better than what I was looking for, I will try to record my printing process to obtain the needed variables. Your generosity will make my job a lot easier. It will probably take me a little while to get it to work for me. I will update you as soon as I see progress.

               

              Again, thank you!

               

              Hector

                • Re: How to print to selected printer and selected tray?
                  Hector Molina

                  Hello Matt,

                   

                  I hope that you are doing well, and thank you for your help. I have been trying to get the macro done in my spare time, I still have not finished it, and have questions. I hope that you can set me straight.

                  The templates that I am working with were created by someone else, and it seems that for sizes "D" and "E" the macro cannot analyze the sheet size. When running the Debug.Print code it returns nothing for it, it's just a blank space after Sheet Size. It is possible that the sheet size is missing, but I am not sure how to put it back, if it can be put back, in the template. I don't even know where that is found. Also, I have tried to record my printing process by staring to record before clicking Print, then I change printer, paper size, orientation, etc. in the dialog boxes, but the macro only seems to record the code below. It seems to be missing information. Can you please advise?

                   

                  Dim swApp As Object

                   

                  Dim Part As Object

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                  Sub main()

                   

                  Set swApp = _

                  Application.SldWorks

                   

                  Set Part = swApp.ActiveDoc

                  Part.PrintDirect

                  Dim myPageSetup As Object

                  Set myPageSetup = Part.PageSetup

                  myPageSetup.PrinterPaperSize = 4099

                  Part.PrintDirect

                  End Sub

                    • Re: How to print to selected printer and selected tray?
                      Matt Martens

                      It appears the sheet size is user specified. Check to see if the size value returned equals 12. If that's the case we could check for sheet size based on the width and height values. You can do this by replacing the 2 0's in the ISheet::GetSize argument with variables of type double.

                      Dim width As Double

                      Dim height As Double

                       

                      Select Case swDrDoc.Sheet(sheetNames(i - 1)).GetSize(width, height)

                      The code will assign values that define the width and height of your sheet. Use these to determine the IPageSetup::PrinterPaperSize to use. Keep in mind the values will be in meters so you will either need to convert them to inches by dividing by .0254 or know the corresponding meter value for the sheet size.

                       

                      As for the code that was recorded,

                      myPageSetup.PrinterPaperSize = 4099

                      is the relevant information you would need for the size sheet you printed. Whichever size you used when recording, "4099" is what you need to assign IPageSetup::PrinterPaperSize for that sheet size.

                        • Re: How to print to selected printer and selected tray?
                          Hector Molina

                          Wow, Matt, thank you for your quick reply.

                          Yes, the size value returned for both "D" and "E" sizes is 12. I will modify the code as per your reply. I will keep you posted. Very helpful information! I appreciate your help.

                            • Re: How to print to selected printer and selected tray?
                              Matt Martens

                              I would suggest editing your sheet format templates to use the "standard sizes" but also add into the Select Case group condition 12 to handle any legacy sheet formats that handle your current situation. The only way to get rid of the 12 condition would be to reload the sheet format with the updated format on every drawing. Obviously you don't want to go through every single drawing so that's how I would handle it.

                            • Re: How to print to selected printer and selected tray?
                              Hector Molina

                              Hi Matt,

                               

                              Sorry for the long delays between my questions, I am trying to do this whenever I get a breather at work. I added the following code as suggested. By stepping into the code I found that it errors out in line "Select Case swDrDoc.Sheet(sheetNames(i - 1)).GetSize(width, height)", it seems to work when I change (sheetNames(i - 1)) to (sheetNames(i)), would that be correct?

                               

                              Also, where would I check for the width or height? Do I check for both?

                               

                              Please forgive my lack of understanding, I thought that I would be able to understand this more easy, but I seem to have more trouble than expected. Thank you for your patience.

                               

                              Dim width As Double

                              Dim height As Double

                              Select Case swDrDoc.Sheet(sheetNames(i - 1)).GetSize(width, height)

                               

                              Select Case swDrDoc.Sheet(sheetNames(i - 1)).GetSize(width, height)

                                  Case 0 '"ANSI A"

                                    sheetSize = "ANSI A"

                                    .HighQuality = False

                                    .PrinterPaperSize = 1

                                    .PrinterPaperSource = 7

                                    printer = printerAB

                                  Case 1 '"ANSI A Vertical"

                                    sheetSize = "ANSI A Vertical"

                                    .Orientation = swPageSetupOrient_Portrait

                                    .PrinterPaperSize = 1

                                    .HighQuality = False

                                    .PrinterPaperSource = 7

                                    printer = printerAB

                                  Case 2 '"ANSI B"

                                    sheetSize = "ANSI B"

                                    .HighQuality = False

                                    .PrinterPaperSize = 17

                                    .PrinterPaperSource = 7

                                    printer = printerAB

                                  Case 3 '"ANSI C"

                                    sheetSize = "ANSI C"

                                    .HighQuality = True

                                    .PrinterPaperSize = 24

                                    printer = printerCDE

                                  Case 4 '"ANSI D"

                                    sheetSize = "ANSI D"

                                    .HighQuality = True

                                    .PrinterPaperSize = 25

                                    printer = printerCDE

                                  Case 5 '"ANSI E"

                                    sheetSize = "ANSI E"

                                    .HighQuality = True

                                    .PrinterPaperSize = 26

                                    printer = printerCDE

                                End Select

                                • Re: How to print to selected printer and selected tray?
                                  Matt Martens

                                  If you start your "i" variable at 0 then yes. I start mine at 1 and because arrays start their index at 0, I have to subtract 1.

                                   

                                  Insert a Case for 12 as shown below. You can check the width, height, or both. If you are able to get the right size based on just one, then try that. If you are going to check both you would need to use an If statement.

                                   

                                  The code below would be added to Select Case statement after the Case 5 grouping. If it doesn't grab the right settings, add a debugging line to see what value the width variable is getting for each sheet size (Debug.Print width / 0.0254). Then adjust the Case values to get the proper settings.

                                   

                                   

                                  Select Case swDrDoc.Sheet(sheetNames(i - 1)).GetSize(width, height)

                                      .....................................

                                      Case 12 '"Custom"

                                        Select Case width / 0.0254

                                          Case 17 '"ANSI C"

                                            sheetSize = "ANSI C"

                                            .HighQuality = True

                                            .PrinterPaperSize = 24

                                            printer = printerCDE

                                          Case 22 '"ANSI D"

                                            sheetSize = "ANSI D"

                                            .HighQuality = True

                                            .PrinterPaperSize = 25

                                            printer = printerCDE

                                          Case 34 '"ANSI E"

                                            sheetSize = "ANSI E"

                                            .HighQuality = True

                                            .PrinterPaperSize = 26

                                            printer = printerCDE

                                        End Select

                                       ......................................

                                    End Select