I have the macro below that works or doesn't depending on which line I use (filepath).
For the filepath which is directed to Z:\1 Plans 2021\MONTIFAUD SAS CHATEAU it works very well, it opens the screw file, inserts the screw in my assembly plan and closes the screw file (Nickel).
While the other filepath which is directed to Z:\SolidWorks\Library doesn' t happen.
I'm on the same server, I'm using the same PRT, it's only the path that's different
Do you have an explanation to give me?
Is there an error in the macro in the declaration of variables?
The purpose of this macro is to integrate it later into Userform.
Thank you in advance.
Kind regards.
MACRO
Dim swPart As SldWorks.ModelDoc2 Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim filePath As String Dim swApp As Object Dim Part As Object Sub main() Set swApp = Application.SldWorks Set swPart = swApp.ActiveDoc Dim tmpObj As ModelDoc2 Dim errors As Long filePath = "Z:\SolidWorks\Library\Screws\Stainless Steel Screws-TH.SLDPRT" 'filePath = "Z:\1 Plans 2021\MONTIFAUD SAS CHATEAU\3D model\Screws\Stainless steel-Screws-TH.SLDPRT" Set tmpObj = swApp.OpenDoc6(filePath, 1, 32, "", errors, longwarnings) Set Part = swApp.ActivateDoc3(filePath, True, 0, errors) Dim swInsertedComponent As Component2 Set swInsertedComponent = swPart.AddComponent5(filePath, 0, "", False, "", 0, 0, 0) swApp.CloseDoc filePath End Sub
Hello Of course you replace the value in your variable! Try this:
Sub Insert
Dim swPart As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, errors, longwarnings As Long
Dim FilePath1, FilePath2 As String
Dim swApp As Object
Dim Part As Object
Dim tmpObj As ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swPart = swApp.ActiveDoc
FilePath1 = "Z:\SolidWorks\Bibliotheque\Visserie\Vis-Inox-TH.SLDPRT"
FilePath2 = "Z:\1 Plans 2021\MONTIFAUD SAS CHATEAU\Modele 3D\Visserie\Vis-Inox-TH.SLDPRT"
Set tmpObj = swApp.OpenDoc6(FilePath1, 1, 32, "", errors, longwarnings)
Set Part = swApp.ActivateDoc3(FilePath1, True, 0, errors)
Dim swInsertedComponent As Component2
Set swInsertedComponent = swPart.AddComponent5(FilePath2, 0, "", False, "", 0, 0, 0)
swApp.CloseDoc FilePath1
End Sub
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2
Dim tmpObj As SldWorks.ModelDoc2
Dim errors As Long
Dim longwarnings As Long
Dim FilePath1 As String
Dim FilePath2 As String
Sub main()
Set swApp = Application.SldWorks
Set swPart = swApp.ActiveDoc
FilePath1 = swPart.GetPathName
FilePath2 = "Z:\SolidWorks\Bibliotheque\Visserie\Vis-Inox-TH.SLDPRT"
Set tmpObj = swApp.OpenDoc6(FilePath2, 1, 32, "", errors, longwarnings)
Set swPart = swApp.ActivateDoc3(FilePath1, True, 0, errors)
Dim swInsertedComponent As Component2
Set swInsertedComponent = swPart.AddComponent5(FilePath2, 0, "", False, "", 0, 0, 0)
swApp.CloseDoc FilePath2
End Sub
In fact, we have to review everything! It is imperative that the file opened be an assembly.
Try this by adding the path of an assembly:
Dim swApp As Object
Dim swModel As ModelDoc2
Dim swAssy As AssemblyDoc
Dim swcomponent As SldWorks.Component2
Dim errors, warnings As Long
Dim FilePath1, FilePath2, FileName, AssemblyTitle As String
Sub Main
Set swApp = Application.SldWorks
FilePathASM = "Chemin de ton assemblage"
FilePathPart = "Z:\SolidWorks\Bibliotheque\Visserie\Vis-Inox-TH.SLDPRT"
FileName = "Vis-Inox-TH.SLDPRT"
Set swModel = swApp.OpenDoc6(FilePathASM, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
AssemblyTitle = swModel.GetTitle
Set tmpObj = swApp.OpenDoc6(FilePathPart, swDocPART, 0, "", errors, warnings)
If warnings = swFileLoadWarning_ReadOnly Then
MsgBox "This file is read-only."
End If
If tmpObj Is Nothing Then
MsgBox "Cannot locate the file."
End If
Set swModel = swApp.ActivateDoc3(AssemblyTitle, True, swUserDecision, errors)
Set swAssy = swModel
Set swcomponent = swAssy.AddComponent5(FileName, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)
End Sub
Be careful though, my macro doesn't handle possible errors so you have to think about adding this famous error handling often neglected ... From my point of view, there are at least 4 checks to be made: 1°) after the line "Set swApp = Application.SldWorks" you have to make sure that a document is open in SW and that this document is indeed an assembly. 2°) after the line "Set swPart = swApp.ActiveDoc" you have to make sure that the assembly opened in SW is not a new unregistered assembly. 3°) after the line "Set swPart = swApp.ActiveDoc" you have to make sure that the assembly opened in SW is not read-only. 4°) after the line "FilePath2 = "Z:\SolidWorks\Library\Screws\Screws-Inox-TH.SLDPRT"" you have to make sure that this file exists.
To answer your initial questions: - Do you have an explanation for me? yes, you activated the "Part" object with the "ActivateDoc3" function but you tried to add the component in the "swPart" object with the "AddComponent5" function.
- Is there an error in the macro in the declaration of variables? there is a lack of homogeneity in the declaration of variables such as either "SldWorks.ModelDoc2" or "ModelDoc2" or "Object" for variables of the same type (swPart, Part and tmpObj).
These are errors that I see quite often because the VBA is quite flexible on the typing of variables and also because of the copy of snippets of code retrieved from the right and left.