This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This macro copies the full path to the selected components into the clipboard using SOLIDWORKS API.
Components can be selected in assembly or drawing document
Components can be selected in the feature tree or in the graphics area
It is also possible to select a component entity (i.e. face or edge) to get the path to the component
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
try_:
OnErrorGoTo catch_
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim path AsStringIfNot swModel IsNothingThenDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swModel.SelectionManager
Dim i AsIntegerFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = NothingIfTypeOf swModel Is SldWorks.AssemblyDoc ThenSet swComp = swSelMgr.GetSelectedObjectsComponent4(i, -1)
ElseIfTypeOf swModel Is SldWorks.DrawingDoc ThenDim swDrawComp As SldWorks.DrawingComponent
Set swDrawComp = swSelMgr.GetSelectedObjectsComponent4(i, -1)
IfNot swDrawComp IsNothingThenSet swComp = swDrawComp.Component
EndIfElse
Err.Raise vbError, "", "Only parts and drawings are supported"EndIfIfNot swComp IsNothingThenIf path <> ""Then
path = path & vbLf
EndIf
path = path & swComp.GetPathName
EndIfNextIf path <> ""Then
Debug.Print path
SetTextToClipboard path
Else
Err.Raise vbError, "", "Please select components"EndIfElse
Err.Raise vbError, "", "Please open document"EndIfGoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSubSub SetTextToClipboard(text AsString)
Dim dataObject AsObjectSet dataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
dataObject.SetText text
dataObject.PutInClipboard
Set dataObject = NothingEndSub
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more