AnsweredAssumed Answered

help with code

Question asked by Roni Ampuja on Jun 12, 2018
Latest reply on Jun 12, 2018 by Roni Ampuja

I have this kind of code to automatically get solidworks drawing name when drawing is not saved and save drawing DXF and PDF.

If the drawing is not saved it will save the drawing with standard name but then the code will want to save the drawing again and this fails. How do I edit this that the secound part will be jumped over if that macro saves the drawing as brand new. And then to skip the first part If the drawing is already saved (this is not nessesary) The code still works if there is already saved solidworksdrawing It just saves the drawing two times and the PDF and DXF. Now I need to run this macro twice First time just to save the drawing. Then I get run-time error 5. Then when i run it again it saves DXF and PDF.

 

Dim swApp               As SldWorks.SldWorks

    Dim swModel             As SldWorks.ModelDoc2

    Dim swModelDocExt       As SldWorks.ModelDocExtension

    Dim swExportData        As SldWorks.ExportPdfData

    Dim boolstatus          As Boolean

    Dim filename            As String

    Dim lErrors             As Long

    Dim lWarnings           As Long

    Dim amountSaved         As Double

Dim err As Long

Dim war As Long

Dim swdraw As SldWorks.DrawingDoc

Dim swsheet As SldWorks.Sheet

Dim swview As SldWorks.View

Dim name As String

 

Sub main()

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    amountSaved = 0

    If swModel Is Nothing Then

        MsgBox "Ei Olemassa Olevaa Tiedostoa", vbCritical

        End

    End If

    If swModel.GetType <> swDocDRAWING Then

        MsgBox "Tämä Macro Toimii Vain Työkuva Näkymässä", vbCritical

        End

    End If

    Set swModelDocExt = swModel.Extension

    Set swExportData = swApp.GetExportFileData(swExportPdfData)

    filename = swModel.GetPathName

    If filename = "" Then

'saves the SLDDRW file save as with standard name if not saved

Set swdraw = swmodel

    Set swview = swdraw.GetFirstView

    Debug.Print swview.name

    Set swview = swview.GetNextView

    Debug.Print swview.name

    Debug.Print swview.GetReferencedModelName

    name = swview.GetReferencedModelName

    name = Left(name, Len(name) - 7)

    Debug.Print name

   

    boolstatus = swmodel.Extension.SaveAs(name & ".slddrw", 0, 1, Nothing, err, war)

   

    End If

    're saves SLDDRW when the SLDDRW already exist

   'Save drawing

    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)

    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

    If boolstatus Then

        amountSaved = amountSaved + 1

    Else

        MsgBox "Työkuvan Tallennus Epäonnistunut, Error code:" & lErrors

    End If

 

    'Save as PDF

    filename = Strings.Left(filename, Len(filename) - 6) & "PDF"

    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)

    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

    If boolstatus Then

        amountSaved = amountSaved + 1

    Else

        MsgBox "PDF Tallennus Epäonnistunut, Error code:" & lErrors

 

    End If

 

    'Save as DXF

    filename = Strings.Left(filename, Len(filename) - 3) & "DXF"

    boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)

    boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

    If boolstatus Then

        amountSaved = amountSaved + 1

    Else

        MsgBox "DXF Tallennus Epäonnistunut, Error code:" & lErrors

    End If

    MsgBox amountSaved & " tiedoston tallennus onnistunut"

 

End Sub

Outcomes