Open Solidworks within Excel

Discussion in 'SolidWorks' started by MaT, Jul 10, 2003.

  1. MaT

    MaT Guest

    I was wondering if there was a way to write a VB macro that will tell
    Excel to open a part in Solidworks and then be able to change
    dimensions within Excel.
     
    MaT, Jul 10, 2003
    #1
  2. You can do this using Sw API or Excel here is a code I used to open an
    excel document and search a row for a specific number it may help get
    you started. What you have to do is in VBA for Excel go up to tools
    References and find the Solidworks References. Once you have
    referenced these files you can program your VBA macro just as you
    would have from Solidworks VBA. It works the other way also. From
    Solidworks VBA add the Excel References.
    The example below Works out of Solidworks


    1) Opens excel
    2) Searches the A column for a string
    3) pulls the cell next to the first found
    4) closes excel (if you use this code make sure you close excel
    otherwise
    you endup having 20 open instances of excel one for each time it
    runs)
    5) returns the value of the cell that was pulled

    Dim exclApp As Excel.Application
    Dim Wkbk As Excel.Workbook
    Dim Sheet As Excel.Worksheet
    Public Cell As String
    Dim actvCell As Range
    Dim text As String

    sub main ()
    msgbox GetMatlDesc ("500253")
    end sub

    Function GetMatlDesc(srchText As String) as String

    If Not Mid(srchText, 6, 1) = "" Then Else Exit Sub
    Set exclApp = Excel.Application

    exclApp.Workbooks.Open ("C:\MATERIAL LIST FROM FS.xls")'create
    this file
    'in the "A"
    column
    'put,
    500253 then
    'next to it
    put,
    'It found
    this desc.

    Set Wkbk = exclApp.ActiveWorkbook
    Set Sheet = exclApp.ActiveSheet
    On Error GoTo ErrHandler

    Cell = Sheet.Range("A:A").Find(srchText).Address(False, False)

    Set actvCell = Sheet.Range(Cell)
    Set actvCell = actvCell.Offset(0, 1)

    Cell = actvCell.text

    Wkbk.Close False
    exclApp.Quit
    Exit Sub
    ErrHandler:
    Cell = "No Material Specified"

    Wkbk.Close False
    exclApp.Quit
    GetMatlDesc = Cell

    End Function
     
    Corey Scheich, Jul 10, 2003
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.