Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Finally I found out myself how to check if the link is already existing... in case it is there it will delete it

I have now a complete  function which saves PDF,DXF and STP of a drawing and attaches the "Export" files to the drawing. So when I check it into the vault all files are there...

 

PDF/DXF/STEP Export is not included in the code below...

 

Option Explicit On
Sub Main()
	'current document
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim oleReference As ReferencedOLEFileDescriptor = Nothing
	Dim selectedfilePDF As String = String.Empty
	Dim selectedfileDXF As String = String.Empty
	Dim selectedfileSTP As String = String.Empty
	
'Verify the current document has been saved.
If doc.FullFileName = "" Then
MessageBox.Show("This document must be saved first.")
Exit Sub
End If

' Link file
Try
selectedfilePDF = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".pdf"
selectedfileDXF = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".dxf"
selectedfileSTP = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".stp"
Catch
Return  'operation was cancelled by the user
End Try
	AddReferencesPDF(doc, selectedfilePDF)
	AddReferencesDXF(doc, selectedfileDXF)
	AddReferencesSTP(doc, selectedfileSTP)
End Sub

'PDF-Attachment
Public Function AddReferencesPDF(ByVal odoc As Inventor.Document, ByVal selectedfilePDF As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefPDF As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsPDF As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefPDF = OLERefsPDF.ItemByName(selectedfilePDF)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefPDF.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefPDF = doc.ReferencedOLEFileDescriptors.Add(selectedfilePDF, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function
'DXF-Attachment
Public Function AddReferencesDXF(ByVal odoc As Inventor.Document, ByVal selectedfileDXF As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefDXF As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsDXF As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefDXF = OLERefsDXF.ItemByName(selectedfileDXF)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefDXF.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefDXF = doc.ReferencedOLEFileDescriptors.Add(selectedfileDXF, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function
'STEP-Attachment
Public Function AddReferencesSTP(ByVal odoc As Inventor.Document, ByVal selectedfileSTP As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefSTP As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsSTP As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefSTP = OLERefsSTP.ItemByName(selectedfileSTP)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefSTP.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefSTP = doc.ReferencedOLEFileDescriptors.Add(selectedfileSTP, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function