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.
- posted
20 years ago
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.
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 pulledDim 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 = CellEnd Function
PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.