Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim swModel As ModelDoc2 Option Explicit Dim swDocExt As ModelDocExtension Dim swAssy As AssemblyDoc Dim tmpPath As String Dim tmpObj As SldWorks.ModelDoc2 Dim boolstat As Boolean Dim strings As Variant Dim swcomponent As SldWorks.Component2 Dim matefeature As SldWorks.Feature Dim MateName As String Dim FirstSelection As String Dim SecondSelection As String Dim Alignment As swMateAlign_e Dim strCompName As String Dim AssemblyTitle As String Dim AssemblyName As String Dim errors As Long Dim warnings As Long Dim mateError As Long Dim doc As SldWorks.ModelDoc2 Dim fileerror As Long Dim filewarning As Long Sub main(oCabineCode As String, SelectedFile As String, TracePath As String) Set swApp = _ Application.SldWorks Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2012\templates\Assemblage.asmdot", 0, 0, 0) Set swModel = swApp.ActivateDoc2(oCabineCode, False, longstatus) Set Part = swApp.ActiveDoc Dim myModelView As Object Set myModelView = Part.ActiveView myModelView.FrameState = swWindowState_e.swWindowMaximized longstatus = Part.SaveAs3(SelectedFile, 0, 2) Set Part = swApp.ActivateDoc2(oCabineCode, False, errors) Dim TmpTitle As String TmpTitle = swApp.ActiveDoc.GetTitle swApp.CloseDoc TmpTitle Set swApp = Application.SldWorks swApp.Visible = True ' Get the current working directory before opening the document Debug.Print "Current working directory is " & swApp.GetCurrentWorkingDirectory Set doc = swApp.OpenDoc6(SelectedFile, swDocASSEMBLY, swOpenDocOptions_Silent, "", fileerror, filewarning) ' Opening a document with SldWorks::OpenDoc6 does not set the working directory Debug.Print "Current working directory is still " & swApp.GetCurrentWorkingDirectory ' Set the working directory to the document directory swApp.SetCurrentWorkingDirectory (Left(doc.GetPathName, InStrRev(doc.GetPathName, "\"))) Debug.Print "Current working directory is now " & swApp.GetCurrentWorkingDirectory TmpTitle = swApp.ActiveDoc.GetTitle Set Part = swApp.ActivateDoc2(TmpTitle, True, errors) ' Open the component Set tmpObj = swApp.OpenDoc6(TracePath, swDocPART, 0, "", errors, warnings) 'Re-activate the assembly so that you can add the component to it Set swModel = swApp.ActivateDoc2(oCabineCode, True, errors) Set swcomponent = swModel.AddComponent5("TRYSW-2016-0006448-1.SLDPRT", swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0) boolstatus = swModel.SetUserPreferenceToggle(swUserPreferenceToggle_e.swDisplayOrigins, True) ' Get the name of the component for the mate strCompName = "TRYSW-2016-0006448-1" ' Create the name of the mate and the names of the planes to use for the mate MateName = "Org_Coin@" + strCompName FirstSelection = "Point1@Origine@" + strCompName & "@" + AssemblyName SecondSelection = "Point1@Origine@" + AssemblyName Set swDocExt = swModel.Extension swModel.ClearSelection2 (True) ' Select the planes for the mate boolstat = swDocExt.SelectByID2(FirstSelection, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault) boolstat = swDocExt.SelectByID2(SecondSelection, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault) ' Add the mate Set matefeature = swModel.AddMate3(0, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError) matefeature.Name = MateName swApp.ActiveDoc.EditRebuild3 swApp.CloseDoc TracePath Set Part = swApp.ActiveDoc TmpTitle = Part.GetTitle Part.ShowNamedView2 "*Isométrique", 7 swModel.ViewZoomtofit2 Part.Save End Sub