ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
GBGrigorii Beliaev07/01/2020

Hello everybody!

I am writing quite simple macro, which is looking for the file in the PDM and opens it.
What I have at the beginning, is the name of the file without extension, so I don`t know, is it Part or Assembly. Also I don`t know precisely, in which folder it is. Because if I know it, then I can simply use swApp.OpenDoc6, which also requires the extension. So, before opening, I have to get the extension of it.

I tried every piece of code from here: https://forum.solidworks.com/thread/223999#comment-888233 , but looke like "EdmVault5" always returns me an "user-defined type is not defined".
FYI, I am using SW 2019.

Do you have any examples of code, solving this problem?

UPDATE:

Great, now this Macro works perfectly. Thanks everybody!
If anybody else will need something similar - here is the code.

Dim swApp        As Object
Dim Part As Object
Dim myModelView As Object

Dim message As String
Dim myVault As New EdmVault5
Dim search As IEdmSearch7
Dim result As IEdmSearchResult5
Dim epdmfile As IEdmFile5
Dim epdmFolder As IEdmFolder5

Dim boolstatus As Boolean
Dim DesiredFile As String
Dim ModelName As String
Dim ModelPath As String

Dim longstatus As Long, longwarnings As Long, longerrors As Long

Sub main()
Set swApp = Application.SldWorks

myVault.LoginAuto "YOURVAULTNAME", 0 '

Set search = myVault.CreateUtility(EdmUtility.EdmUtil_Search)
search.Clear

DesiredFile = "45052-03" '------HERE ADJUST THE MODEL NAME -----------

search.FindFolders = False
search.FindFiles = True
search.FileName = DesiredFile & ".%"
search.Recursive = True
search.SetToken Edmstok_Recursive, True

Set result = search.GetFirstResult

Set epdmfile = Nothing
Set epdmFolder = Nothing

While Not result Is Nothing

Set epdmFolder = myVault.GetObject(EdmObjectType.EdmObject_Folder, result.ParentFolderID)
Set epdmfile = myVault.GetObject(EdmObjectType.EdmObject_File, result.ID)
Set result = search.GetNextResult()
ModelName = epdmfile.Name

If Right(ModelName, 3) = "PRT" Then

ModelPath = epdmFolder.LocalPath & "\" & epdmfile.Name
Set Part = swApp.OpenDoc6(ModelPath, 1, 0, "", longstatus, longwarnings) 'Opening the .SLDPRT model

ElseIf Right(ModelName, 3) = "ASM" Then

ModelPath = epdmFolder.LocalPath & "\" & epdmfile.Name
Set Part = swApp.OpenDoc6(ModelPath, 2, 0, "", longstatus, longwarnings) 'Opening the .SLDASM model
End If
'message = message & vbCrLf & epdmfile.Name & " at " & epdmFolder.LocalPath

Wend
Set Part = swApp.ActiveDoc

' HERE IS YOUR CODE

' Close the file
swApp.CloseDoc ModelName

End Sub