ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
CACad Admin14/06/2018

The following beginning of mycode below runs fine the first time, but errors out on the second, if i open it up in the debugger, and close, it will run again once then not the second.  I get the proverbial  "Run-time error 462 : The remote server machine does not exist or is unavailable”

Basically the code opens an excel doc that is identically named to the Solidworks file that is open.

I've looked at a lot of articles on it here and other cites...  Ive tried the getobject vs createobject, late binding, and a bunch of other things.  My head is spinning.

Ironically stepping through the code on the second run (i know it will fail), it WILL, open excel, it WILL open the file, it fails on the line xlSheet.Sort.SortFields.Add...

Function excel_main()

   

    Dim SwApp                                   As SldWorks.SldWorks

    Dim xlApp                                   As Excel.Application

    Dim xlWorkbook                              As Excel.Workbook

    Dim xlSheet                                 As Excel.Worksheet

    Dim swModel                                 As SldWorks.ModelDoc2

    Dim swCustPropMgr                           As SldWorks.CustomPropertyManager

    Dim Fname                                   As String

    Dim xlOpen                                  As String

    Set SwApp = Application.SldWorks

    Set swModel = SwApp.ActiveDoc

   

    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)

        Fname = swCustPropMgr.Get("Name")

        xlOpen = Fname + ".xls"

   

    On Error Resume Next

    Set xlApp = CreateObject("Excel.Application")

    On Error GoTo 0

    If xlApp Is Nothing Then Set xlApp = CreateObject("Word.Application")

   

    xlApp.Visible = True

   

    Set xlWorkbook = xlApp.Workbooks.Open(xlOpen)

       

    Set xlSheet = xlApp.Worksheets(Fname)

    xlSheet.Sort.SortFields.Add _

        Key:=Range("A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _

        :=xlSortTextAsNumbers