Override drawing dimension based on excel value

I have used the below code in solidworks macro to override the dimension and its working fine, when I used same code in excel vba I am getting errors. Anyone please help me on this.

Code which I used,

Dim swApp as SldWorks.SldWorks

Dim Part as ModelDoc2

Dim SelMgr as SelectionMgr

Dim DispDim as DisplayDimension

Dim theVal as Double

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part1@DV2","DIMENSION",0,0,0, FALSE,0, Nothing,0)

Set DispDim = SelMgr.GetSelectedObject6(1,0)

theVal = (50 * 25.4) / 1000

DispDim.SetOverride True, theVal

End Sub


API ​​​​​​​

Macro ​​​​​​​

Comments 3
JB
JB 2021-10-21

You have to use GetObject or CreateObject to get the swApp variable.  Application.Sldworks only works from a SolidWorks macro. "Application" means "The thing running the code".  Since Excel is running the code, SldWorks is not a thing you can get from the Excel application object.  You also have to go to Tools->References in the VBA editor and add references to the SolidWorks libraries.

LE
LE 2021-10-22

Thank you Josh. I have updated the code based on your suggestion, but I am not sure about the selection manager and also not familiar with vba. So can you please help me on this?



Dim swApp as Object

Dim Part as Object

Dim boolstatus as Boolen

Dim SelMgr as SelectionMgr

Dim DispDim as DisplayDimension

Dim theVal as Double

Sub main()

Set swApp = CreateObject("Sldworks.Application")

Set Part = swApp.NewDocument("C:\Solidworks2015\templates",0,0,0)

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part1@Drawing View2","DIMENSION",0,0,0, FALSE,0, Nothing,0)

Set DispDim = SelMgr.GetSelectedObject6(1,0)

theVal = (50 * 25.4) / 1000

DispDim.SetOverride True, theVal

End Sub

JB
JB 2021-10-24

No, sorry.  There are loads of tutorials and training materials on VBA readily available on the internet.  I am far to lazy to write you a custom one here.