I want to communicate parameters between Solidworks and Excel (.xlsx) back and forth using VBA macros. Currently, this is what my Excel looks like:
For now, my script inserts the A2 and B2 values into Solidworks. Manually, I extruded the sketch and gave the sketch x mm depth. How can I implement the x mm depth back into Excel?
This is my Macro1 script so far:
' C:\Users\Laptop\AppData\Local\Temp\swx3584\Macro1.swb
' ******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim myLength As Object, myLength2 As Object
Dim myWidth As Object
Dim Value As Integer, Value2 As Integer
'MAIN FUNCTION
Sub TEST()
'Access active Solidworks and Excel files
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set xl = GetObject(, "Excel.Application")
Set xlsh = xl.ActiveSheet
'Select cells (Rows, Colums)
Value = xlsh.Cells(2, 2)
Value2 = xlsh.Cells(2, 1)
'Import new dimension from Excel
Part.EditSketch
Set myLength = Part.Parameter("D1@Sketch1")
myLength.SystemValue = Value / 1000
Part.SketchManager.InsertSketch True
boolstatus = Part.EditRebuild3()
'Import new dimension from Excel
Part.EditSketch
Set myLength2 = Part.Parameter("D2@Sketch1")
myLength2.SystemValue = Value2 / 1000
Part.SketchManager.InsertSketch True
boolstatus = Part.EditRebuild3()
End Sub
I googled but could only find clear examples of communicating Excel --> Solidworks instead of the other way around. Thank you in advance. If something is not clear, please let me know!
This will write the value back to excel.
Note that there are a lot of assumptions here. The feature is a boss-extrude named Boss-Extrude1. There is no file type validation, Excel must be open, it will write the value on the active sheet to that specific cell...
Sub WriteBackToExcel()
'Access active Solidworks and Excel files
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set xl = GetObject(, "Excel.Application")
Set xlsh = xl.ActiveSheet
Set myLength = Part.Parameter("D1@Boss-Extrude1")
xlsh.Cells(2, 3).Value = myLength.SystemValue * 1000
End Sub
You will note that the code is kind of similar, it only changes the flow of information.