Macro for opening a plane with only part of the 3D file name

Hello

I would like to make a macro that would allow me to open the plan of the room in which I launch the macro. However, the name of the MEP is only a part of the 3D file name.

My 3D files are called "3D0000M000-1 File Name. Sldprt" and my plans are only called "3D0000M000-1".

They are located in the same folder.

I can't start a search with only these first 12 characters.

If anyone has an idea I'm interested, I'm sure it's not complicated but I'm blocking!

Thanks in advance!

Hello

Normally you only need two clicks to open your MEP, CTRL-D + entered  or see the letter that is affected ( file == > create a plan and next to it there is the shortcut)


ctrl-d.jpg
1 Like

Hello

A bit of work on vba strings, see HERE.

Kind regards

 

2 Likes

Hello Gentlemen,

Thank you for your feedback!

AC Cobra 427 : Since the part is not called exactly like the plane, it does not open.

D.Roger: I tried to integrate the Left function but I get an error 91 when I run it. I think it's the swModel variable that's the problem, but I tried with swModel.GetPathName and it doesn't change anything.

Here's my code:


code_ouverture_plan.jpg

Hello

Filename = Left(swModel.GetTitle, 12)

Kind regards

Thank you d.roger!

I don't have any more compilation errors but unfortunately the principle doesn't work. The macro sends me the message that it can't find a plan when there is one in the same folder.

Any idea what I could have done wrong?

Hello

You need to retrieve the path of the folder, work on the file name and then add the desired extension:

Dim Filename As String
Filename = Left(swModel.GetTitle, 12)
Debug.Print Filename
Dim swPath As String
swPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\", , 0))
Debug.Print swPath
Dim FilenamePlan As String
FilenamePlan=swPath & Filename & ".slddrw"
Debug.Print FilenamePlan

Kind regards

Small as "beast"; Why change the name of the MEP???

Hello 

Your macro returns the message that there is no drawing because you put *.slddrw   so it looks for 3D0000M000-1*.slddrw

Hello

Thank you for your help!

I finally solved my problem but I hadn't taken the time to tell you!

Here's the code I used:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2

Dim DocName As String
Dim DrwName As String

Dim swLoadErrors As Long
Dim swLoadWarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

DocName = swModel.GetTitle

DrwName = Left$(DocName, 12) & ".slddrw"

DrwName = Replace(swModel.GetPathName, DocName, DrwName, 1, , vbTextCompare)

If Dir$(DrwName) <> "" Then

Set swModel = swApp.OpenDoc6(DrwName, swDocDRAWING, swOpenDocOptions_Silent, "", swLoadErrors, swLoadWarnings)

End If

End Sub