Page 1 of 1

How to Paste a property value inside of an excel cell

Posted: Fri Jun 18, 2021 2:50 pm
by ResidentAtLarge
I am pretty new to the API with very little programming experience.
I've looked at the solidworks API Help and I am not sure on which command to use.

My code that I currently have copies from a specific cell and puts the value into the custom property that I've defined.

What I would like to do is to copy the value of a custom property and paste it in a specific cell of the workbook first. then copy the value of a different cell back to a different solidworks property. I have the last part, but I'm looking to do the first part.

Any help would be fantasic! Thank you!

Here is the code that I have.

Code: Select all

'Dim swApp As Object

'Dim Part As Object
'Dim boolstatus As Boolean
'Dim longstatus As Long, longwarnings As Long

'Option Explicit

Dim xlRange As Object

Dim swCustPrpMgr As SldWorks.CustomPropertyManager

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim xlApp As Object

Dim xlSheet As Object

Dim xlBooks As Object

' Open Excel workbook

Sub Main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True

xlApp.Workbooks.Open "C:\STOCKED INVENTORY LIST.xls"

Set xlSheet = xlApp.ActiveSheet

' Copy data from Excel to Custom Properties

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

' Set xlApp = GetObject(, "Excel.Application")

' Set xlSheet = xlApp.Sheets(1)

Name = xlSheet.Cells(8, 9)

swModel.DeleteCustomInfo2 "", "RawPartNo"

swModel.AddCustomInfo3 "", "RawPartNo", swCustomInfoText, Name

Name = xlSheet.Cells(9, 9)

swModel.DeleteCustomInfo2 "", "RawOperation"

swModel.AddCustomInfo3 "", "RawOperation", swCustomInfoText, Name

' Close Excel

xlApp.Workbooks.Close

xlApp.Quit

End Sub

Re: How to Paste a property value inside of an excel cell

Posted: Fri Jun 18, 2021 3:27 pm
by matt
ResidentAtLarge wrote: Fri Jun 18, 2021 2:50 pm I am pretty new to the API with very little programming experience.
I've looked at the solidworks API Help and I am not sure on which co...
@ResidentAtLarge , we have code tags you can use for code in the main editor. I fixed this one.

Re: How to Paste a property value inside of an excel cell

Posted: Sun Jun 20, 2021 12:21 pm
by gupta9665
You just need to use the reverse of below line

Code: Select all

Name = xlSheet.Cells(8, 9)
i.e.

Code: Select all

 xlSheet.Cells(8, 9) = "Your desired value here"

Re: How to Paste a property value inside of an excel cell

Posted: Mon Jun 21, 2021 2:28 pm
by ResidentAtLarge
gupta9665 wrote: Sun Jun 20, 2021 12:21 pm You just need to use the reverse of below line

Code: Select all

Name = xlSheet.Cells(8, 9)
i.e.

Code: Select all

 xlSheet.Cells(8, 9) = "Your desired value here"
As someone new to programming, I did not think to reverse it the way you put it. But, this is the correct way to do it. I have successfully got this script to work the way I wanted it to.

I used the API help to find the correct way to copy a custom property here: http://help.solidworks.com/2017/english ... Redirect=1

Thank you Gupta!