0 Replies Latest reply on Oct 20, 2016 5:25 PM by Jamey Evans

    Use vba in Excel to print Solidworks drawing using Edrawings?

    Jamey Evans

      I have a spreadsheet that is used to make job packets.  I have written the code listed below to print the drawing that corresponds to the part listed on the work order.  This is running from Excel and works great in Solidworks but our operations group is using Edrawings.  Can anyone help me with the syntax to make it work with Edrawings api?  I haven't been having much luck on my own and am relatively new to VBA.

       

       

      Dim swApp As SldWorks.SldWorks
      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swModelDocExt As SldWorks.ModelDocExtension
      Dim swPageSetup As SldWorks.PageSetup
      Dim printSpec As SldWorks.PrintSpecification
      Dim drawing As String
      Dim errors As Long
      Dim warnings As Long
      Dim footerTextNumber As String
      Dim footerTextCount As String
      Dim footerText As String
      
      
      
      Option Explicit
      Sub printsldworks()
          Set swApp = CreateObject("SldWorks.Application")
          swApp.Visible = False
          
          Call FindFilePath 'Sub takes part number from production sheet and finds filepath for drawing "tgtDir"
          
          If Len(tgtDir) > 0 Then 'If drawing exists then print
              drawing = tgtDir
              Set swModel = swApp.OpenDoc6(drawing, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
          
              'Set up page's footer text
              Set swModelDocExt = swModel.Extension
              swModelDocExt.UsePageSetup = swPageSetupInUse_e.swPageSetupInUse_Document
                  Set swPageSetup = swModel.PageSetup
                  footerTextNumber = swPageSetup.GetHeaderFooterString(swStandardHeaderFooterPageSetupTexts_e.swHeaderFooterText_PageNumber)
                  footerTextCount = swPageSetup.GetHeaderFooterString(swStandardHeaderFooterPageSetupTexts_e.swHeaderFooterText_PageCount)
                  footerText = (footerTextNumber & " of " & footerTextCount)
                  swPageSetup.CenterFooter = footerText
              'Create a print specification
              Set printSpec = swModelDocExt.GetPrintSpecification
              printSpec.ConvertToHighQuality = True
              printSpec.AddPrintRange 1, 1
              printSpec.FromScale = 1.5
              printSpec.ToScale = 0.5
              printSpec.ScaleMethod = swPrintSelection
              printSpec.PrinterQueue = ""
              printSpec.PrintToFile = False
          
              Debug.Print "Printing specifications:"
              Debug.Print "  Collate: " & printSpec.Collate
              Debug.Print "  Convert to high quality: " & printSpec.ConvertToHighQuality
              Debug.Print "  Current sheet: " & printSpec.CurrentSheet
              Debug.Print "  From scale: " & printSpec.FromScale
              Debug.Print "  To scale: " & printSpec.ToScale
              Debug.Print "  Number of copies: " & printSpec.NumberOfCopies
              Debug.Print "  Print background: " & printSpec.PrintBackground
              Debug.Print "  Print cross hatch on out-of-date views: " & printSpec.PrintCrossHatchOnOutOfDateViews
              Debug.Print "  Printer queue: " & printSpec.PrinterQueue
              Debug.Print "    File: " & printSpec.PrintFile
              Debug.Print "  Print white items black: " & printSpec.PrintWhiteItemsBlack
              Debug.Print "  Selection as defined in swPrintSelectionScaleFactor_e: " & printSpec.ScaleMethod
              Debug.Print "  Total sheet count: " & printSpec.SheetCount
              Debug.Print "  X origin: " & printSpec.XOrigin
              Debug.Print "  Y origin: " & printSpec.YOrigin
          
              ' Print the drawing to your default printer
              swModelDocExt.PrintOut4 "", "", printSpec
              printSpec.RestoreDefaults
              printSpec.ResetPrintRange
              swApp.QuitDoc swModel.GetTitle
          
          Else
              MsgBox ("Print Not Found for " & tgtFile)
              
          End If
          
      End Sub
      

       

      Thanks,

      Jamey