Solidworks PDM API Search in Datacard (SOLVED)

Hello everyone,

I would like to create a macro that search for a part number that is stored in the Datacard. And then return its name and path.

I tried with Edmutility_Search with Edmstok_ContentTextInProperties set to “True”, but it doesn’t work.

Is there an other way of searching for it ?

Here is the code I tried it with

Public swApp As SldWorks.SldWorks

Sub main()

Dim myVault      As New EdmVault5
Dim search       As IEdmSearch7
Dim result       As IEdmSearchResult5
Dim epdmfile     As IEdmFile17
Dim epdmFolder   As IEdmFolder12
Dim DesiredFile  As String
Dim ModelName    As Variant
Dim Path As String

    ' Login'
    If Not myVault.IsLoggedIn Then myVault.Login "abcd", "efgh", "ijklm"

Set swApp = Application.SldWorks
Set search = myVault.CreateUtility(EdmUtility.EdmUtil_Search)

DesiredFile = "10000008" ' Part number we wish to find'

search.FindFolders = False
search.FindFiles = True
search.FileName = DesiredFile
search.Recursive = True
search.SetToken Edmstok_Recursive, True
search.SetToken Edmstok_ContentTextInProperties, True ' do content search in the file custom properties.'

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)
    ModelName = epdmfile.Name
    Path = epdmfile.GetLocalPath(epdmFolder.ID)

End Sub
1 Like

The file name is the “12345.SLDPRT” used by the file system. If you want to use search.FileName then you’ll have to add the extension to the text of the file name.

I’m not sure how you intend to use this so I can’t really make any further recommendations.

1 Like

I think i explained myself badly…

I don’t have the file name, i have a list of “article number” and need to find their file name and path. The PDM Search does it (see picture) but i need to make a lot of times so it’s why we want to write a macro doing it.

The code I wrote is a copy of one my boss made for an other application, and im quite a beginer in PDM api. The “search.FileName = DesiredFile” is i think wrong.

That does explain things a bit more. I think you should remove the FileName line.

Is the number you’re searching for in the same field on the data card every time? If so, do you know the variable name associated with that field?

If you aren’t sure, the “Preview” tab within PDM can clarify that as well.

I imagine that adding the following line would get you what you need:

search.AddVariable("YourDataCardVariableName", DesiredFile);

Thanks, I think that is exactly what i was looking for … But it gets a syntax error every way i tried to write it.

Here is the new Code.

Test = "DT-Art.-Nr."
DesiredFile = "31000235" ' Part number we wish to find

search.FindFolders = False
search.FindFiles = True
search.AddVariable(Test, DesiredFile)
'search.FileName = DesiredFile
search.Recursive = True
search.SetToken Edmstok_Recursive, True
search.SetToken Edmstok_ContentTextInProperties, True

Sorry, I pasted it from a similar programming language (c#)

Yours should be in VBA:
search.AddVariable Test, DesiredFile

it’s working thanks

Hahah no worries, should have come to this one on my one, sorry it’s the end of the day for me.

You really save my day by the way, can’t thank you enough

1 Like