How do you get out of macro?

Hello

In a macro that allows you to save import files, how do you manage if the user clicks on the "Cancel" button in the "Save As" window?

Macro Details:

Sub main()

.....

        swApp.SetCurrentWorkingDirectory "D:\Studies\Commerce" 'path
        swApp.RunCommand SwCommands.swCommands_SaveAs, ""

If the "Cancel" button clicked by user, then exit sub also not continue the macro.

...

end sub

Thank you in advance for the advice and answers
 

Hello 

The RunCommand API allows you to run SW commands but you can't get any feedback. it's like manually clicking on the button manually in SW

Why don't you run a save-as dialog box yourself and then use the SaveAs Method API (IModelDocExtension) to export your file.

A+

Philippe 

Thank you for the answer...

I lack VBA skills to understand everything you advise me, but I'll take a closer look at it.

A+

Not enough knowledge in VBA, so I don't really see how to launch this dialog on my own, and looking at the SW API help, I'm not any more advanced.

I will need to know a little more about the method to follow.

Thank you.

Hello

Post your code.

Here's the code in a txt file.


save_import.txt

Hello

@ prossignol :

How should the dialog box be launched?

If I look in SW's help, there is not much: http://help.solidworks.com/SearchEx.aspx?q=dialog+box+vba&sort=&version=2018&lang=English&prod=api&context=LANG%3den%26NRESULTS%3d25

Hello

As prossignol said, you need to use the SaveAs Method API (IModelDocExtension) to export your file. For the dialog box you can just make a msgBox with choice and process what to do according to the user's feedback. Here is an example of what you can do in your macro in the action for a room

ElseIf (swModel.GetType = swDocPART) Then
    swModel.SetTitle2 "$PRP:SW-File Name"
    Dim namePRT As String
    Dim chemin As String
    chemin = "D:\Etude\Commerce\"
    namePRT = chemin & swModel.GetTitle & ".sldprt"
    If MsgBox("Voulez-vous sauvegarder cette pièce ?", vbYesNo, "Enregistrement") = vbYes Then
        swModel.SaveAs namePRT
    End If
End If

You will find on the link: https://www.excel-pratique.com/fr/vba/boites_de_dialogue.php more information on the possibilities of VBA dialogs.

Kind regards

1 Like

Hello

Thank you for these answers, but I want to keep the window open with windows explorer because there is a tree structure in this "Commerce" folder to store the components.

Sorry, but at the moment, I don't see a better answer....

Kind regards.

Hello

In this case, it is possible to open an "explorer" without using the SW dialog window. It's something more generic and allows you to navigate through the tree.

Hello

In this case, I'm taking the snippet of code, because even if I managed to find a solution with msgbox, a generic solution would suit me well....

Kind regards.

Hello

You can also use a BrowserForFolder to allow the user to choose the destination folder:

ElseIf (swModel.GetType = swDocPART) Then swModel.SetTitle2 "$PRP:SW-File Name"
    Dim namePRT As String
    Dim chemin As String
    chemin = "D:\Etude\Commerce\"
    If MsgBox("Voulez-vous sauvegarder cette pièce ?", vbYesNo, "Enregistrement") = vbYes Then
        Dim objShell   As Shell
        Dim ssfWINDOWS As Long
        Dim objFolder  As Folder
        Set objShell = New Shell
        Set objFolder = objShell.BrowseForFolder(0, "SelectDossiers", 0, chemin)
        If (Not objFolder Is Nothing) Then
            namePRT = objFolder & "\" & swModel.GetTitle & ".sldprt"
            swModel.SaveAs namePRT
        End If
    End If
End If

 

Be careful, you have to put "Microsoft Shell Controls And Automation" in the project references.

Kind regards

1 Like