Problem on solidworks macro

Hello

 

I'm not very good at programming, that's why I'm coming to see you. 

 

I try to compile two macros in order to create STEP from the MEP so I have a first macro that opens the drawing room and a second one that saves the part in STEP format.

 

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 version AP214
boolstatus = swApp.SetUserPreferenceIntegerValue(swStepExportPreference, swAcisOutputGeometryPreference_e.swAcisOutputAsSolidAndSurface) 'Force export in Solid/Surface Geometry format


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      <------ Compile Error
Titel = Left(Titel, (Len(Titel)))


    If retval = Naam Then
        nRetval = swApp.SendMsgToUser2(Naam_aangepast & " . STEP File Has Been Created", 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
 

Macros work when they're alone but as soon as I compile it it doesn't work anymore. I get the following message displayed "Compilation error: variable not defined"  (see the line above in bold)

 

If anyone can help me.

 

Thanks in advance!

HI

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

Dim Titel as String

David

1 Like

Thanks David, i will try this.

I have another problem, the macro doesn't launch when I click on the shortcut I created but it works when I'm in the editor and click on "Play".

Do you have a 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 Like

I managed to solve my problem, I copied the macro into a new macro, it's there as if by magic it works!