4 Replies Latest reply on Mar 11, 2016 9:50 AM by Mike Smith

    Why does LinkSource not work in a SolidWorks Macro

    Mike Smith

      I am trying to use LinkSources to retrieve the links to an external spreadsheet from an internal Design Table after opening it in a new window but I keep getting a Debug Error..."Run-Time error '91': Object variable or With block variable not set". I have used this in an Excel macro and it works fine but not in the SolidWorks marco. I have added the Microsoft Excel 16.0 Object Library to my references. Here is my code for reference

       

      Option Explicit

       

       

      Dim swApp           As SldWorks.SldWorks

      Dim Part            As SldWorks.ModelDoc2

      Dim boolstatus      As Boolean

      Dim longstatus      As Long, longwarnings As Long

      Dim newLinkPath     As Variant

      Dim currentLinks    As Variant

      Dim swModel         As SldWorks.ModelDoc2

      Dim swDT            As SldWorks.DesignTable

       

      Sub main()

      Set swApp = _

      Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      'Get the current working directory

      newLinkPath = CurDir$

       

      Set Part = swApp.ActiveDoc

       

      'Open the design table to allow for updating

      boolstatus = Part.Extension.SelectByID2("Design Table", "DESIGNTABLE", 0, 0, 0, True, 0, Nothing, 0)

      Part.InsertFamilyTableEdit

       

      'Update Design Table link to external spreadsheet

      Dim xlApp           As Excel.Application

      Dim xlWB            As Excel.Workbook

      Dim xlWS            As Excel.Worksheet

      Dim currentLinks    As Variant

      Set xlApp = New Excel.Application

      Set xlWB = ActiveWorkbook

      Set xlWS = ActiveSheet

       

      currentLinks = ActiveWorkbook.LinkSources(xlExcelLinks)

      Dim i As Integer

      If Not IsEmpty(currentLinks) Then

          For i = 1 To UBound(currentLinks)

              MsgBox "Link " & i & ":" & Chr(13) & currentLinks(i)

          Next i

      End If

            

      Dim myModelView As Object

      Part.CloseFamilyTable

      End Sub