1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Sub Macro3()
'Déclaration des variables :
Dim choix_liste As String 'type de pièce choisi dans la liste déroulante pour l'ouverture des pièces
Dim nb_de_piece As Integer 'nombre de lignes de la colonne F correspondant aux nombres de pièces
Dim increment As Integer 'incrément de 1 pour effectuer la boucle de recherche ligne par ligne sur la colonne F
Dim myCell As String 'variable pour fusionner l'incrément et la colonne F pour la plage de recherche
Dim path As String 'chemin du dossier d'enregistrement de la pièce
Dim nom As String 'nom de la pièce
Dim path_complete As String 'chemin complet de la pièce
Dim myBool As Boolean
Dim myError As Long, myWarning As Long
'Initialisation de certaines variables:
choix_liste = Range("E6")
nb_de_piece = Application.WorksheetFunction.CountA(Feuil1.Range("$I:$I")) 'nombre de cellules non vide dans la colonne F, soit le nombre de pièces
increment = 1
'ouverture de SolidWorks :
myBool = Shell("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe", vbNormalFocus)
Application.Wait Now + TimeValue("0:00:10")
'Set swApp = CreateObject("SldWorks.Application")
'definition de la plage sur lequel va s'appliquer la macro :
For increment = 1 To nb_de_piece
myCell = "I" & (increment + 9) 'concatène le nom de la cellule à partir de F5 pour l'utiliser avec Range()
If Range(myCell) = choix_liste Then 'si le type de pièce en colonne F correspond au choix de la liste alors
path = Range("J" & (increment + 9)) 'extraction du chemin du dossier
nom = Range("G" & (increment + 9)) 'extraction du nom de la pièce
path_complete = path + nom & ".sldprt" 'création du chemin complet avec extension
Range(myCell).Activate 'Selectionne la cellule = action à effectuer pour vérification pas à pas
Set swApp = CreateObject("SldWorks.Application") 'ouverture SolidWorks
Set swModel = swApp.OpenDoc6(path_complete, 1, 1, "", myError, myWarning) 'ouverture fichier
'swModel.SaveAs2 Left(path, (Len(path) - 6)) & "step", 0, True, False 'Enregistrement en STEP
'swApp.CloseDoc path
Set swApp = Nothing: Set swModel = Nothing
End If
Next increment 'on passe à la ligne suivante
End Sub
'Code récupéré sur internet pour ouverture pièce SolidWorks avec chemin complet :
'Sub Bouton1_Cliquer()
'Dim myBool As Boolean
'Dim myError As Long, myWarning As Long
'myBool = Shell("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe", vbNormalFocus)
'Set swApp = CreateObject("SldWorks.Application")
'Set swModel = swApp.OpenDoc6("C:\Users\SONFVER\Desktop\macro\test\01.sldprt", 1, 1, "", myError, myWarning)
'Set swApp = Nothing: Set swModel = Nothing
'End Sub |
Partager