Hi everyone,
I'm trying to write a VBA macro that reads part/assembly names from a .txt file and inserts them into an active SolidWorks assembly.
The files are stored in a SOLIDWORKS PDM vault. I know the exact folder structure where they are stored (e.g. C:\\SOLID\\PDM\\STANDARDS\\I-INSERTS\\I-03407\\I-03407-20.SLDASM).
I'm using EdmVault5 and IEdmFile5 to try to download the latest version of a file using:
vb
KopiujEdytuj
edmFile.GetFileCopy2 edmFolder.ID, edmFile.CurrentVersion, EdmGet_Refresh Or EdmGet_Simple
But I keep getting errors like:
"The latest version of the file is not available on the archive server." or "Object doesn't support this property or method."
I already call vault.LoginAuto("MyVault", 0) and vault.GetFolderFromPath(...), and I get valid folder/file references.
💡 Is there a reliable way in VBA to make sure a file is downloaded or available locally, given the full path inside the vault?
My current code
Const EdmGet_Simple As Long = &H1
Const EdmGet_Refresh As Long = &H2
Sub InsertFromQuickInsertFile()
On Error GoTo ErrHandler
Dim swApp As Object
Dim swAssembly As Object
Dim comp As Object
Dim vault As EdmLib.EdmVault5
Dim edmFile As EdmLib.IEdmFile5
Dim edmFolder As EdmLib.IEdmFolder5
Dim folderPath As String
Dim filePath As String
Dim fileName As String
Dim insertPrefix As String
Dim fullModelPath As String
Dim line As String
Dim fileNumber As Integer
Dim lines() As String
Dim i As Integer
Dim foundFile As Boolean
Dim InputFilePath As String
Dim DesktopPath(0 To 3) As String
Dim lineCount As Integer
Dim fileContent As String
' Inicjalizacja SolidWorks
Set swApp = Application.SldWorks
Set swAssembly = swApp.ActiveDoc
If swAssembly Is Nothing Then MsgBox "Brak aktywnego złożenia w SolidWorks.": Exit Sub
' Szukanie pliku QuickInsert.txt
DesktopPath(0) = Environ("USERPROFILE") & "\\Desktop\\QuickInsert.txt"
DesktopPath(1) = Environ("USERPROFILE") & "\\Pulpit\\QuickInsert.txt"
DesktopPath(2) = Environ("OneDrive") & "\\Desktop\\QuickInsert.txt"
DesktopPath(3) = Environ("OneDrive") & "\\Pulpit\\QuickInsert.txt"
For i = 0 To 3
If Dir(DesktopPath(i)) <> "" Then
InputFilePath = DesktopPath(i)
Exit For
End If
Next
If InputFilePath = "" Then
MsgBox "Na pulpicie brak pliku QuickInsert.txt", vbCritical, "Błąd"
Exit Sub
End If
' Odczyt danych z pliku
fileNumber = FreeFile
Open InputFilePath For Input As #fileNumber
lineCount = 0
Do While Not EOF(fileNumber)
Line Input #fileNumber, line
If Trim(line) <> "" Then
lineCount = lineCount + 1
ReDim Preserve lines(1 To lineCount)
lines(lineCount) = Trim(line)
End If
Loop
Close #fileNumber
If lineCount = 0 Then
MsgBox "Plik QuickInsert.txt jest pusty.", vbCritical, "Błąd"
Exit Sub
End If
' Połączenie z PDM
Set vault = New EdmLib.EdmVault5
vault.LoginAuto "PDM", 0
' Przetwarzanie każdej linii
For i = 1 To lineCount
fileName = lines(i)
If InStr(fileName, "-") = 0 Then GoTo NextFile
insertPrefix = Left(fileName, InStrRev(fileName, "-") - 1)
folderPath = "C:\\SOLID\\PDM\\STANDARDS\\I-INSERTS\\" & insertPrefix
filePath = folderPath & "\\" & fileName & ".sldasm"
Set edmFolder = vault.GetFolderFromPath(folderPath)
If edmFolder Is Nothing Then
Debug.Print "Folder nie znaleziony: " & folderPath
GoTo NextFile
End If
Set edmFile = vault.GetFileFromPath(filePath)
If edmFile Is Nothing Then
Debug.Print "Plik nie znaleziony: " & filePath
GoTo NextFile
End If
' Pobierz najnowszą wersję z PDM
edmFile.GetFileCopy2 edmFolder.ID, edmFile.CurrentVersion, EdmGet_Refresh Or EdmGet_Simple
' Wstaw do złożenia
fullModelPath = edmFolder.LocalPath & "\\" & fileName & ".sldasm"
Set comp = swAssembly.AddComponent(fullModelPath, 0, 0, 0)
Debug.Print "Dodano: " & fullModelPath
NextFile:
Next i
MsgBox "Zakończono dodawanie insertów.", vbInformation
Exit Sub
ErrHandler:
MsgBox "Błąd: " & Err.Description
End Sub
Thanks in advance!
