Grigorii Beliaev

Search and open the file from PDM (VBA)

Discussion created by Grigorii Beliaev on Jan 7, 2020
Latest reply on Jan 10, 2020 by Mario Zahren

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: , 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?




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)

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
Set Part = swApp.ActiveDoc


' Close the file
swApp.CloseDoc ModelName

End Sub