Probleme sur macro solidworks

Bonjour,

 

Je suis pas très bon dans la programmation, c'est pour cela que je viens vous voir. 

 

J'essaye de compiler deux macros afin de pour créer des STEP à partir de la MEP donc j'ai une première macro qui ouvre la pièce de la mise en plan et une second qui enregistre la pièce en format STEP.

 

im swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Locatie As String
Dim Locatie_aangepast As String
Dim OpenDoc As Object
Dim Extensie_nieuw As String
Dim Extensie_oud As String
Dim retval As String
Dim Naam As String
Dim Naam_aangepast As String
Dim bRetVal As String
Dim swModel     As SldWorks.ModelDoc2
Dim swDrawing   As SldWorks.DrawingDoc
Dim swView      As SldWorks.View
Dim Errors      As Long

Sub macro1()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swApp = CreateObject("SldWorks.Application")
Set OpenDoc = swApp.ActiveDoc()
Set Part = swApp.ActiveDoc

' Is document active?
If swModel Is Nothing Then
swApp.SendMsgToUser2 "A Drawing file must be open.", swMbWarning, swMbOk
Exit Sub
End If

' Is it a part document?
If swModel.GetType <> SwConst.swDocDRAWING Then
swApp.SendMsgToUser2 "A Drawing file must be open.", swMbWarning, swMbOk
Exit Sub
End If

Set swDrawing = swModel
Set swView = swDrawing.GetFirstView
Set swView = swView.GetNextView
If swView Is Nothing Then
MsgBox "Please insert a Model View first!"
End
Else
swApp.ActivateDoc3 swView.GetReferencedModelName, False, swRebuildOnActivation_e.swUserDecision, Errors
End If

Call macro2
End Sub


Sub macro2()


Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set OpenDoc = swApp.ActiveDoc()
Set Part = swApp.ActiveDoc

boolstatus = swApp.SetUserPreferenceIntegerValue(swStepAP, 214) 'Force la version AP214
boolstatus = swApp.SetUserPreferenceIntegerValue(swStepExportPreference, swAcisOutputGeometryPreference_e.swAcisOutputAsSolidAndSurface) 'Force l'export en format Solid/Surface Geometry


Extensie_oud = ".SLDPRT"
Extensie_nieuw = ".STEP"
Locatie = OpenDoc.GetPathName
Locatie_aangepast = Left(Locatie, Len(Locatie) - 7)
retval = Dir$(Locatie_aangepast & Extensie_oud)
Naam = Dir$(Locatie)
Naam_aangepast = Left(Naam, Len(Naam) - 7)
Titel = OpenDoc.GetTitle      <------ Erreur de compilation
Titel = Left(Titel, (Len(Titel)))


    If retval = Naam Then
        nRetval = swApp.SendMsgToUser2(Naam_aangepast & " .STEP Le fichier a été créé", swMbWarning, swMbOk)
    End If
    
    
Set Part = swApp.ActiveDoc
longstatus = Part.SaveAs3(Naam_aangepast + Format(Now, " yyyy-mm-dd") & Extensie_nieuw, 0, 0)
swApp.CloseDoc Titel

End Sub
 

Les macros fonctionne quand elles sont seules mais des que je l'ai compil ca ne fonctionne plus. J'ai le message suivant qui s'affiche " Erreur compilation : variable non définie "  (voir la ligne ci-dessus en gras)

 

Si quelqu'un peut m'aider.

 

Merci d'avance !

HI,

Title is not in your Dim list and is therefore not defined you will need to add:

Dim Titel as String

David

1 « J'aime »

Thanks David, i will try this.

J'ai un autre problème, la macro ne se lance pas quand je clique sur le raccourci que j'ai créé mais elle fonctionne quand je suis dans l'editeur et que je clique sur "Play".

Avez vous une solution ?

I would check the details in the shortcut setup (I am assuming this is a SOLIDWORKS Button?)

If the macro is stored on a network drive could you copy it locally and then update your button to point to the local copy? Just to make sure network permissions aren't causing the issue.

David

1 « J'aime »

J'ai réussis a solutionner mon problème, j'ai copié la macro dans une nouvelle macro, est la comme par magie ça fonctionne !