File save dialog

Please cooperate

A macro to select the folder to save the file is required, but the initial folder cannot be specified with "BrowseForFolder Method".

Is it possible to specify the initial folder or create a dialog to open the last opened folder with VBA?

SolidworksApi/macros
Answers 4
DG
DG 2021-03-26 Best Answer

You can get the current working directory  

And you can pass that variable to Browse For Folder codes. 

Alternatively you can hard code the initial folder in following codes

Function BrowseFolder(Optional Caption As String, Optional InitialFolder As String) As String

Dim SH As Shell32.Shell

Dim F As Shell32.Folder

 

InitialFolder= "Put required path here"

Set SH = New Shell32.Shell

Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, InitialFolder)

If Not F Is Nothing Then

    If F = "Desktop" Then

        BrowseFolder = Environ("USERPROFILE") & "\Desktop"

    Else

        BrowseFolder = F.Items.Item.Path

    End If

End If

End Function

YF
YF 2021-03-26

Thank you very much.
I was completely able to do what I was looking for based on the code I was taught.
If you have any other questions, thank you again.

Option Explicit

Public swApp As SldWorks.SldWorks
Public swDoc As SldWorks.IModelDoc2
Public MyFolder As String
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long


Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Dim Path As String

Path = BrowseFolder("", swApp.GetCurrentWorkingDirectory)

If Path = "" Then
MsgBox "Please select the path and try again"
End
Else
Path = Path & "\"
End If

End Sub

Option Explicit
Private Const BIF_RETURNONLYFSDIRS As Long = &H1

Function BrowseFolder(Optional Caption As String, Optional InitialFolder As String) As String

Dim SH As Shell32.Shell

Dim F As Shell32.Folder

MyFolder = Left(swDoc.GetPathName, InStrRev(swDoc.GetPathName, "\") - 1)
InitialFolder = Left(MyFolder, InStrRev(MyFolder, "\") - 1)


Set SH = New Shell32.Shell

Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, InitialFolder)

If Not F Is Nothing Then

If F = "Desktop" Then

BrowseFolder = Environ("USERPROFILE") & "\Desktop"

Else

BrowseFolder = F.Items.Item.Path

End If

End If

End Function

DG
DG 2021-03-26

福 谷 勇 時 wrote:

Deepak Gupta
I'm sorry many times
Please tell me the code of sub main()

Try the following codes.

Sub main()

Set swApp = Application.SldWorks
Path = BrowseFolder("", swApp.GetCurrentWorkingDirectory)

If Path = "" Then
MsgBox "Please select the path and try again"
End
Else
Path = Path & "\"
End If


End Sub

YF
YF 2021-03-26

Deepak Gupta
I'm sorry many times
Please tell me the code of sub main()