You can use the get selected object from the drawing and then use that further to open the drawings.
Try the initial codes from this macro: Re: How do I get component name by selectionmanager When activedoc is drawing
I have tried this code on a assembly drawing. I clicked on one of the components in the drawing and ran the macro. However its giving me a string as shown below. The filename is at the end of the string with a trailing "-1". Is there a proper code to use to get the exact filename? The one covered by the last black line?
Note: The first two black lines are the assembly filename, while the last black line is the component filename. My goal is to click on the hatched area of the component in an assembly drawing and run the macro to open the drawing. Thanks guys!!!!!!! Really appreciate it. I do this so often
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDrawComp As SldWorks.DrawingComponent
Dim i As Integer
Dim strName As String
Set swApp = _
If swApp.GetDocumentCount() = 0 Then Exit Sub
Set swModel = swApp.ActiveDoc
If Not swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then
MsgBox ("You Must Run This Macro Inside Of A Drawing")
Set swSelMgr = swModel.SelectionManager
If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then Exit Sub
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Set swDrawComp = swSelMgr.GetSelectedObjectsComponent4(i, 0)
If Not swDrawComp Is Nothing Then
strName = swDrawComp.Name
MsgBox ("The Name Of The Selected Component Is: " + strName)
That whole string in the message box is a filename or is it multiple files?
What I did was, on an assembly drawing car.slddrw, I clicked on one of the components (the wheel) and ran the macro. The name obtained is the string above. What I needed was the name of the component which is "wheel", with the end goal of opening wheel.slddrw (without opening wheel.sldprt). Is this possible? I tried your code Dimitry but for some reason theres a compiling error. But I am pretty sure its cuz I messed something up cuz my knowlege on this is limited. Thanks for all your help guys!
Deepak! Thanks a lot, the macro Open Selected Component Drawing - (Assy).swp.zip works really well. If you could come up with a macro that opens the drawing of a selected part in a assembly drawing, that will be perfect
Open Selected Component Drawing macro in above post opens the selected component drawing from assembly drawing. One need to select the component from feature tree. The component drawing should be in folder and same name.
Here is updated version where one can select the component from view and drawing opens up.
The macro you have created opens individual components in assembly. What would I have to do to create a macro that will open drawing component model from any view or open assembly model. The reason I ask is because I want update configuration name in solidworks right away when I open drawing.
I wrote this a while to print all of the drawings in an open assembly.
Here's how to find a drawing of a part (note: dwgPath and dwgfPath are strings):
Private Sub Print_Drawing()
Dim swapp As Object
Dim boolstatus As Boolean
Dim Part2 As Object
Dim longstatus As Long, longwarnings As Long
Set swapp = Application.SldWorks
dwgPath = swdoc.GetPathName ' Get the path of the current part
dwgfPath = (Strings.Left(dwgPath, Strings.Len(dwgPath) - 6)) & "SLDDRW" ' Find the drawing associated with that part
Set Part2 = swapp.OpenDoc6(dwgfPath, 3, 0, "", longstatus, longwarnings) ' Open the drawing
If (Part2 Is Nothing) Then
' do nothing
Set Part2 = swapp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part2.ActiveView
Dim ps As PageSetup
Set ps = Part2.PageSetup
ps.Orientation = swPageSetupOrientation_e.swPageSetupOrient_Landscape ' Set the orientation to landscape
Part2.PrintDirect ' Print the drawing
swapp.CloseDoc src ' Close the drawing
Or just click on the part and select open drawing.
As Chris stated, you can just select the part and select the "Open Drawing" icon. I am using SW 2015 and I think it was there in 2014 too.
This is only if you are trying to open the drawing of one of the components in the assembly (part or subassembly).
Or you can right click on the component from within the FeatureManager Tree and get a similar list of icons that also has the Open Drawing Icon.