Here we are using screws with specific numbers. These screws are stored according to their number on the network. I try to make a macro that retrieves the number in an excel and then inserts the said screw in the active assembly. So far I manage to get the screw to open but impossible to insert, can you help me?
Thank you
Sub ouvrirdanssolidworks()
'
'Option Explicit
Dim swApp As Object, swModel As Object
Dim myError As Long, myWarning As Long
Dim swAssembly As Object
Set swApp = CreateObject("SldWorks.Application")
If swApp.GetDocumentCount = 0 Then
MsgBox "Aucun fichier ouvert"
Set swApp = Nothing
End If
Set swModel = swApp.ActiveDoc
'If swModel.GetType <> swDocASSEMBLY Then
'MsgBox "Le document actif n'est pas un assemblage"
'End
'End If
a = ActiveCell.Value
b = "C:\EPDM_VAULT1\1 000 00x-Fournitures\"
c = 1
d = Val(Right(ActiveCell.Value, 3))
e = Left(ActiveCell.Value, 6)
If Val(Right(ActiveCell.Value, 3)) > 499 Then c = 2
AssemblyTitle = swModel.GetTitle
Set swAssembly = swModel
If c = 1 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "000-" & e & "499\" & a & ".sldprt"
If c = 2 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "500-" & e & "999\" & a & ".sldprt"
Set swModel = swApp.OpenDoc6(fichier, 1, 0, "", myError, myWarning)
If c = 1 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "000-" & e & "499\" & a & ".prt"
If c = 2 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "500-" & e & "999\" & a & ".prt"
Set swModel = swApp.OpenDoc6(fichier, 1, 0, "", myError, myWarning)
'insérer
swApp.ActivateDoc2 AssemblyTitle, False, longstatus
'Set Part = swApp.ActiveDoc
'Set swComponent = swAssembly.AddComponent(AssemblyTitle, "", 0, 0, 0)
End Sub
Otherwise, since Addcomponent is obsolete from an API point of view, look at AddComponent5 Method (SW2016 possible that it doesn't exist on your version)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim myError As Long
Dim myWarning As Long
Dim swAssembly As SldWorks.ModelDoc2
Dim fichier As String
Dim swcomponent As SldWorks.Component2
Sub ouvrirdanssolidworks()
Set swApp = CreateObject("SldWorks.Application")
If swApp.GetDocumentCount = 0 Then
MsgBox "Aucun fichier ouvert"
Set swApp = Nothing
End If
Set swModel = swApp.ActiveDoc
AssemblyTitle = swModel.GetTitle
fichier = "C:\xxx\xxx.sldprt"
Set swModel = swApp.OpenDoc6(fichier, 1, 0, "", myError, myWarning)
Set swModel = swApp.ActivateDoc3(AssemblyTitle, True, swUserDecision, myError)
Set swAssembly = swModel
' Add the part to the assembly document
Set swcomponent = swAssembly.AddComponent5(fichier, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)
End Sub
it works but I had to replace the "SldWorks.ModelDoc2" with "object"
Do you think I could do the same thing but from what is stored in the press paper? Like if I copy a number it opens it with the macro (if it exists of course)? Where can I get the value of the clipboard?
Sub ouvrirdanssolidworks()
Dim swApp As Object 'ici
Dim swModel As Object 'ici SldWorks.ModelDoc2
Dim myError As Long
Dim myWarning As Long
Dim swAssembly As Object 'ici SldWorks.ModelDoc2
Dim fichier As String
Dim swcomponent As Object 'ici SldWorks.Component2
Set swApp = CreateObject("SldWorks.Application")
If swApp.GetDocumentCount = 0 Then
MsgBox "Aucun fichier ouvert"
Set swApp = Nothing
End If
Set swModel = swApp.ActiveDoc
AssemblyTitle = swModel.GetTitle
a = ActiveCell.Value
b = "C:\EPDM_VAULT1\1 000 00x-Fournitures\"
c = 1
d = Val(Right(ActiveCell.Value, 3))
e = Left(ActiveCell.Value, 6)
If Val(Right(ActiveCell.Value, 3)) > 499 Then c = 2
If c = 1 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "000-" & e & "499\" & a & ".sldprt"
If c = 2 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "500-" & e & "999\" & a & ".sldprt"
Set swModel = swApp.OpenDoc6(fichier, 1, 0, "", myError, myWarning)
'If c = 1 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "000-" & e & "499\" & a & ".prt"
'If c = 2 Then fichier = "C:\EPDM_VAULT1\1 000 00x-Fournitures\" & e & "500-" & e & "999\" & a & ".prt"
'Set swModel = swApp.OpenDoc6(fichier, 1, 0, "", myError, myWarning)
extensionTitle = swApp.ActiveDoc.GetTitle
Set swModel = swApp.ActivateDoc3(AssemblyTitle, True, swUserDecision, myError)
Set swAssembly = swModel
' Add the part to the assembly document
Set swcomponent = swAssembly.AddComponent5(extensionTitle, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0, 0, 0)
End Sub