How to Paste a property value inside of an excel cell

Programming and macros
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

How to Paste a property value inside of an excel cell

Unread post 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
User avatar
matt
Posts: 1537
Joined: Mon Mar 08, 2021 11:34 am
Answers: 18
Location: Virginia
x 1158
x 2294
Contact:

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

Unread post 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.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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

Unread post 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"
Deepak Gupta
SOLIDWORKS Consultant/Blogger
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

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

Unread post 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!
Post Reply