4 Replies Latest reply on May 2, 2018 2:48 PM by Amen Allah Jlili

    API Loop for each open document

    Frédéric Leclercq

      Hello

      I'm new user here.

       

       

      I'm an french user so , sorry for lot of errors

       

      I copied and modified an existing API about printing.

      I open a drawing and the macro select the good printer according to the format.

       

      It's ok for one document.

       

       

      But now, I want to modify this API to print all open drawings

      I don't understand where to put the loop

       

      during my tests, it prints 2 or 3 times the same drawing

       

       

      My single code below (dirty I know )

       

      Thanks for your help

       

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long

      Dim Annotation As Object

      Dim Gtol As Object

      Dim DatumTag As Object

      Dim FeatureData As Object

      Dim Feature As Object

      Dim Component As Object

      Dim Paperformat As Variant 'defining paperformat

      Dim Printer_no1 As Variant 'defining printer no. 1 (laserprinter)

      Dim Printer_no2 As Variant 'defining printer no. 2 (plotter)

      Dim A4 As Variant

      Dim A3 As Variant

      Dim A2 As Variant

      Dim A1 As Variant

      Dim A0 As Variant

       

      Dim swAppl As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swDraw As SldWorks.DrawingDoc

          Dim swSheet As SldWorks.Sheet

          Dim vSheetProps As Variant

          Dim bRet As Boolean

       

      Const swPrintPaperSize = 0

      Const swPrintOrientation = 1

      Const vbPRORPortrait = 1

      Const vbPRORLandscape = 2

       

      Sub print_selected_paperformat() 'macro modified by FL

       

      Dim swApp As Object

      Dim Part As Object

              

      'Set printer path and name

      Let Printer_no1 = "HP LaserJet 5200 Series PCL 5" 'nom de l'imprimante A3 dans l'explorateur Windows récupéré

      Let Printer_no2 = "HP Designjet 500 42+HPGL2 Card at //dlink-9324C3/dlk-9324C3-P1" 'plotter HP plans A1 A0

      'Set paperformats

      Let A4 = 9

      Let A3 = 8

      Let A2 = 54

      Let A1 = 621

      Let A0 = 622

       

       

      Set swApp = CreateObject("SldWorks.Application")

      Set Part = swApp.ActiveDoc

       

       

      Set swAppl = Application.SldWorks

          Set swModel = swAppl.ActiveDoc

          Set swDraw = swModel

          Set swSheet = swDraw.GetCurrentSheet

       

       

      vSheetProps = swSheet.GetProperties

         

          MsgBox (vSheetProps(0)) 'aperçu du numéro si OK

       

       

      Paperformat = vSheetProps(0)

       

      If Paperformat = 8 Then GoTo printformat_a4 Else

      If Paperformat = 8 Then GoTo printformat_a3 Else

      If Paperformat = 9 Then GoTo print_fit_a3 Else

      If Paperformat = 10 Then GoTo printformat_a1 Else

      If Paperformat = 11 Then GoTo printformat_a0 Else

      If Paperformat = 5 Then GoTo print_fit_a3 Else GoTo End_routine

      End

       

      'subroutines depending on papersize

      printformat_a4:

      Printer = Printer_no1

      Part.PrintSetup(swPrintOrientation) = vbPRORPortrait

      Let Paperformat = A4 '#9 = A4 on laserprinter laserjet 5200

      swApp.ActivePrinter = Printer

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat A4

      GoTo Print_finally_drawing

       

       

      printformat_a3:

      Printer = Printer_no1

      Part.PrintSetup(swPrintOrientation) = vbPRORLandscape

      Let Paperformat = A3 '#8 = A3 on laserprinter laserjet 5200

      swApp.ActivePrinter = Printer

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat = A3

      GoTo Print_finally_drawing

       

       

      printformat_a2:

      Printer = Printer_no1

      Part.PrintSetup(swPrintOrientation) = vbPRORLandscape

      swApp.ActivePrinter = Printer

      Let Paperformat = A3

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat A3 instead of A2

      GoTo Print_finally_drawing

       

       

      printformat_a1:

      Printer = Printer_no2

      Part.PrintSetup(swPrintOrientation) = vbPRORLandscape

      swApp.ActivePrinter = Printer

      Let Paperformat = A1

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat A1

      GoTo Print_finally_drawing

       

       

      printformat_a0:

      Printer = Printer_no2

      Part.PrintSetup(swPrintOrientation) = vbPRORLandscape

      Let Paperformat = A1

      swApp.ActivePrinter = Printer

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat A1

      Part.PrintOut2 1, 1, 1, False, Printer, 0#, False, ""

      GoTo End_routine

       

      print_fit_a3:

      Printer = Printer_no1

      Part.PrintSetup(swPrintOrientation) = vbPRORLandscape

      Let Paperformat = A3

      swApp.ActivePrinter = Printer

      Part.PrintSetup(swPrintPaperSize) = Val(Paperformat) 'Enable selected Paperformat A3

      'Part.PrintPreview 'ONLY FOR TEST

      Part.PrintOut2 1, 1, 1, False, Printer, 0#, False, ""

      GoTo End_routine

       

      Print_finally_drawing:

      'Part.PrintPreview 'ONLY FOR TEST

      Part.PrintOut2 1, 1, 1, False, Printer, 1#, False, ""

      GoTo End_routine

       

      End_routine:

      End Sub

       

       

      'Values for the Laserjet 5200

      'Value  Paper size

      '8 A3 (297 x 420 mm)

      '9 A4 (210 x 297 mm)

      '54 A2

       

      'Values for the traceur HP

      'Value  Paper size

      '621 A1 (594 x 841mm)

      '622 A0 (841 x 1189mm)