VBA Assign an EDMVAULT5 instance with the "Late Binding" technique

@yves-marie.freyssinet 

OK... But if instead of just taunting the community, you could share your solution with them to help them as they help you, it would be generous of you.

1 Like

I'm not taunting anyone! I hadn't finished my macro yet.... But everything is good and functional:)  So I am happy to share what I have found and I hope that the time I have spent searching will be beneficial to others!  

Here is the macro I managed to make by doing late blinding as binoyte explains, it is used to extract a file via the PDM script: 

Sub main()

Dim Vault As Object
Set Vault = CreateObject("ConisioLib.EdmVault")

Vault.LoginAuto "NOM_COFFRE_PDM", 0

modelPath = "<Filepath>"
    
    If Vault.IsLoggedIn Then
    
        Dim folder As Object
        Dim folderPath As String
        Dim FolderPath1 As String
        folderPath = modelPath
        j = InStrRev(folderPath, "\")
        FolderPath1 = Left(folderPath, j)

    Set folder = Vault.GetFolderFromPath(FolderPath1)
    
      i = Len(modelPath)
      j = InStrRev(modelPath, "\")
      FileName = Right(modelPath, i - j)

  Dim file As Object
  Dim oNull As Object
  Set file = folder.GetFile(FileName)
    
    If file Is Nothing Then

        MsgBox "File not found."
    End If


  file.LockFile folder.ID, 0
  CheckOutFile = "Successful"
        
        
    Else
        Err.Raise vbError, "User is not logged in to the vault"
    End If


End Sub

**Don't forget to replace the 'NOM_COFFRE_PDM' with your pdm vault name:) 

1 Like

Sorry for the mistake but this kind of answer is so recurrent on the net...

Bravo for the macro and Thanks for sharing;)

1 Like

Thank you. So  what makes it work?

You are bothered with the treatment of your paths. You should use the Scripting.FileSystemObject library

' Chargement de l'objet Système de Fichiers 
Dim oFS As Object
Set oFS = CreateObject("Scripting.FileSystemObject")

sInputFile = "<Filepath>"

Look in the object explorer for other methods in this library and its parent, Scripting, in general. It's pretty practical!

1 Like

From what I read and could find as info, once I created the following object: 

Dim Vault As Object
Set Vault = CreateObject("ConisioLib.EdmVault")

just declare for example  "Folder As Object" instead of "Folder As IEdmFolder5" and it works, the same for other statements of the same type:) 

Thank you very much for the info about the processing of file paths, I'll take a closer look at it!! I'm not an expert in VBA macro yet ahah and I'm glad I have some advice:) 

Looking forward to sharing more tips! 

Ah, when I open my eyes, I understand better why it works for you and not for me!

You use the method:

Vault.GetFolderFromPath()

 

which has never been a problem for me by the way, but in my case, I use the method:

Vault.GetFileFromPath()

 

And there it doesn't work. So my problem remains.

Here is my minimal example:

Sub ajoutfich()
    Dim eVault, eFile, eFolder As Object
    Dim iStatus As Integer
    Dim sFichier, sDestination, sSource As String
    
    Dim oFS As Object
    Set oFS = CreateObject("Scripting.FileSystemObject")
    
    sDestination = "D:\ePDM\…\dossier\"
    sSource = "E:\FICHIER.EXT"
    sFichier = oFS.buildpath(sDestination, oFS.GetFileName(sSource))

    Set eVault = CreateObject("ConisioLib.EdmVault")
    If Not eVault.IsLoggedIn Then
        eVault.LoginAuto "MON_COFFRE_PDM", 0
    End If

    Set eFolder = eVault.GetFolderFromPath(sDestination)
    
    Set eFile = eVault.GetFileFromPath(sFichier)
    If Not eFile Is Nothing Then
        'à complèter
    End If
    
    iStatus = eFolder.AddFile(0, sSource)
End Sub

 

I'm getting runtime error 13: type mismatch for row:

Set eFile = eVault.GetFileFromPath(sFichier)

 

However, the casting seems to be done automatically. When we look at the VBA spy window (see attached screenshot),  we can see IEdmVault5 objects. Also, the eVault.LoginAuto() method  depends on the IEdmVault5.

So why is it stuck on eVault.GetFileFromPath()?


1659427953.screenshot.png

Hello

One argument is missing:

Set eFile = eVault.GetFileFromPath(sFichier, eFolder)

 

Hello

Isn't it supposed to be optional?

 

EDIT: out of conscience, I tested as you indicated, same mistake!

It doesn't seem to me, except to put 0

Hello

No problem with me with the eFolder argument, otherwise same error by having just sFile

What if you try using the following statement instead of the "Set eFile = eVault.GetFileFromPath(sFile)"?

Dim efolder As Object
Dim sFichier As Object

Set eFile = efolder.GetFile(sFichier)

Does it work or not?

The doc page: GetFileFromPath Method (IEdmVault5)

It's optional, and I had also tried with 0.

Thank you for your answer but... damn! I'm wondering if I should take a look at the dll recording with regsrv32.

I think about this because I tried to create an EdmFile object directly:

Set eFile = CreateObject("ConisioLib.EdmFile")

and I get a run-time error 429: an activeX component cannot create an object.

1 Like

I admit that this may be a little beyond my skills ahaha but I'm still available if you manage to unblock or move forward:) 

Well, my minimal example has fallen into play!

So indeed the second argument is not optional. 

Set eFile = eVault.GetFileFromPath(sFichier, eFolder)

 

The only other manipulation I performed was a

regsrv32 "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS PDM\EdmInterface.dll"

 

which may have put the dll record back in order. EdmInterface.dll was necessarily loaded,  otherwise I would have had many other messages... It remains to be seen how it behaves in an ePDM task!

I don't like to close without having understood everything, but hey THANK YOU VERY MUCH for your help!

2 Likes

Maybe the PDM API documentation is fake as well. Even without doing "Late Binding" I always put an argument after the memory file path.

Duplicate, I can't delete

1 Like

I think the documentation shows the intent of the developers, but the implementation in the function is wrong.

The second argument is passed to the function by reference, which allows it to access it  by writing. To be clear, this argument does not specify the directory in which to look for the file. It simply allows you to recover the directory object in which the file was found. But as we look for a file by its path, we already know the parent directory. This second argument is absolutely not essential. Maybe we can gain a few lines of code in some cases.

On the other hand, be careful if the file does not exist, the function will put Null in the second argument. In my minimal example, getFileFromPath can empty eFolder by assigning it to Null, and crash the rest. Well, that was just an example.