AnsweredAssumed Answered

ctrl+s does not work after saving file with macro

Question asked by Stijn Helsen on Aug 19, 2020
Latest reply on Aug 20, 2020 by Artem Taturevych

Hi all,


I made my first macro and it seems to work just fine. However I have one problem that i can not solve.


The macro creates a new drawing from a part/assembly based on a drawing template. I added some msgbox in case the file already exists. This al works fine. But when trying to save the newly created or existing file with the save button (or ctrl-s), after running te macro, the 'browse-for-file-dialog' pops up instead of just saving the file.


It looks like the 'session' is not aware of the 'save-action' in my macro. So it reacts like the file was not saved before. If you know what I mean...


Dim swApp As Object
Dim Model As SldWorks.ModelDoc2
Dim Part As Object
Dim Part2 As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

'Bepalen van path en bestandnamen
Dim MyPathName As String
Dim MyPath As String
Dim MyFolder As String
Dim MyFile As String
Dim MyPrePath As String
Dim MyTemplatePath As String
Dim MyDrawingPath As String

MyPathName = Part.GetPathName
MyPath = Left(Part.GetPathName, InStrRev(Part.GetPathName, "\") - 1)
MyPrePath = Left(Part.GetPathName, InStrRev(Part.GetPathName, "\Google Drive\") - 1)
MyFolder = Right(MyPath, Len(MyPath) - InStrRev(MyPath, "\"))
MyFile = Right(Part.GetPathName, Len(Part.GetPathName) - InStrRev(Part.GetPathName, "\"))

MyTemplatePath = MyPrePath & "\Flemac bvba\SW Design Team - SW Library\SW Drawing Templates\Flemac - A3 Portrait.drwdot"
MyDrawingPath = Left(Part.GetPathName, Len(Part.GetPathName) - 6) & "SLDDRW"

Debug.Print "----------------------------------------------------" & vbCrLf
Debug.Print "   My Path & name =  '" & MyPathName & "'"
Debug.Print "   My Template path ='" & MyTemplatePath & "'"
Debug.Print "   My path =         '" & MyPath & "'"
Debug.Print "   My folder =       '" & MyFolder & "'"
Debug.Print "   My file =         '" & MyFile & "'"
Debug.Print "   Drawing path =    '" & MyDrawingPath & "'" & vbCrLf

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(MyDrawingPath) Then
    If MsgBox("Drawing already exhists. Do you want to open the exhisting file?", vbYesNo, "Exhisting Drawing") = vbYes Then
        'Open exhisting document
        Set Part2 = swApp.OpenDoc(MyDrawingPath, swDocDRAWING)
        ' Create New Document
        Dim swSheetWidth As Double
        swSheetWidth = 0.297
        Dim swSheetHeight As Double
        swSheetHeight = 0.42
        Dim swDrawing As DrawingDoc
        Set swDrawing = swApp.NewDocument(MyTemplatePath, 12, swSheetWidth, swSheetHeight)
        Dim swSheet As sheet
        Set swSheet = swDrawing.GetCurrentSheet()
        swSheet.SetProperties2 12, 13, 1, 20, False, swSheetWidth, swSheetHeight, True
        swSheet.SetTemplateName (MyTemplatePath)
        swSheet.ReloadTemplate True
        swDrawing.InsertModelInPredefinedView (MyPathName)
        Dim myModelView As Object
        Set myModelView = swDrawing.ActiveView
        myModelView.FrameState = swWindowState_e.swWindowMaximized

        If MsgBox("Would you like to overwrite the exhisting file?", vbYesNo, "Overwrite") = vbYes Then
            ' Save As
            longstatus = swDrawing.SaveAs3(MyDrawingPath, 0, 2)
        End If
    End If
End If

End Sub


This is my first post here. So please forgive me if I do something stupid ;-). I obviously took some codes from examples I I found on the internet.


Hope someone here can help me with this.