1 Reply Latest reply on Apr 10, 2014 10:14 AM by Jesse Seger

    how to import values from excel file

    Vishaldeep Singh

      hello

      i want to take the co-ordinates values of a point from excel file and take the values untill blank row comes.

       

      i was trying one code but there is some error i am writin the code below can any one debug it.

      the code will run only when the excel file is open.

       

       

      '''''' code'''''''

      Dim swApp As Object

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim Excel As Object

      Dim i As Integer

      Dim xpt As Double

      Dim ypt As Double

      Dim zpt As Double

      Sub main()

       

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

       

                  Excel = GetObject(, "Excel.Application")

                  swModel = CType(swApp.ActiveDoc, ModelDoc2)

                  swModel.SketchManager.Insert3DSketch (True)

                  Dim skPoint As SketchPoint

                  swModel.SketchManager.AddToDB = True

       

       

              Do While Excel.cells(i, 1).Text <> ""

       

       

                  xpt = Excel.cells(i, 1).Value

                  ypt = Excel.cells(i, 2).Value

                  zpt = Excel.cells(i, 3).Value

                  skPoint = swModel.SketchManager.CreatePoint(xpt, ypt, zpt)

                  i = i + 1

       

       

              Loop

              swModel.SketchManager.InsertSketch (True)

              swModel.SketchManager.AddToDB = False

      End Sub

        • Re: how to import values from excel file
          Jesse Seger

          You're very close.  Here is the updated code.  Make sure you add a Reference to the Microsoft Excel Library in Tools -> References.

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSketchMgr As SldWorks.SketchManager

          Dim swExcel As Excel.Application

          Dim exSheet As Excel.Worksheet

          Dim i As Integer

          Dim xpt As Double

          Dim ypt As Double

          Dim zpt As Double

          Sub main()



              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc


              Set swExcel = GetObject(, "Excel.Application")

              Set exSheet = swExcel.ActiveSheet



              Set swModel = swApp.ActiveDoc

              Set swSketchMgr = swModel.SketchManager

              swSketchMgr.Insert3DSketch (True)

              Dim skPoint As SketchPoint

              swSketchMgr.AddToDB = True



              i = 1

              Do While exSheet.Cells(i, 1).Value <> ""

                  xpt = exSheet.Cells(i, 1).Value

                  ypt = exSheet.Cells(i, 2).Value

                  zpt = exSheet.Cells(i, 3).Value

                  Set skPoint = swSketchMgr.CreatePoint(xpt, ypt, zpt)

                  i = i + 1

              Loop


              swSketchMgr.InsertSketch (True)

              swSketchMgr.AddToDB = False

          End Sub