VB code for for activating the save dialog

Sep 21, 2005 12 Replies

Can somebody tell me which code should be used to pop up the "save as" dialog box and fill the field with something like "test.sldprt"?



All I want to do is give the user the possibility to change the proposed file name into something else. So I have to pop up the save dialog box instead of saving the document immediately.



It seems to me something very simple, but I couldn't find any documentation about it.



I asked this while ago from the API support and...it's not possible to fill in the filename...so I guess there is no point to show the dialog either (that is possible I guess).

Try to look at the CommonDialog Control and program your "own" saveas. It should be possible to fill in your desired filename. If it is not available you have to make a reference to it - RMB in toolbox (forms) and it should be in the list if you have the right file "C:WINNT\system32\comdlg32.ocx". Not sure but maybe you have to install Visual Basic first.

Good luck Thomas

Unfortunately I need VB6 for this. And I don't have VB6. And even when I had VB6, I had to program a complex form with all the options which are now at the standard save as dialog box. I am afraid I don't have the programming skills to make this.

Maybe a freeware tool to do the job?

TVO schreef:

There is a way to invoke Common Dialog with VBA/macro. It involves using Windows API. I have done this many times.

Try a Google search using keywords: VBA API Windows Common Dialog.

Learning some Windows API manipulation can really extend your macro capability by allowing you to call objects that are normally only available to licensed VB6 users. Yes, it's legal.

yep, Not with the SW APIs but Win32 API I did it "long time ago" with sw03

formatting link
you need vb6 and bit skills to subclass SW save as dialog

Mike

Unfortunately, there is no download link for you SWMenuini.zip...

snipped-for-privacy@gmail.com schreef:

Maybe something like this:

Option Explicit

Dim swApp As Object Sub main() Dim i As Integer Set swApp = Application.SldWorks SendKeys "%{F}" 'invoke file menu For i = 0 To 6 'go down to the saveas dialog SendKeys "{down}" Next i SendKeys "{enter}" 'enter End Sub

ok, not very elegant but it does the job.

M.

This is only a part of the solution, because the file name in the file name field should be filled with someting dynamic. And this is not possible with the standard save as dialog.

Just guessing, but would it be possible to remove the standard save & saveAs menu items and replace it with your own dialog?

Vinodh Kumar M.

formatting link

I guess you can't use SaveAs or anything like that with own dialog...so how you tell to it what you want to save...is the any API for this, "TheWholeModelThatIsSaveble" :-) No idea. I still suggest that you go around this problem and don't use commondialog or similar, but SaveAs and msgboxes to ask the user for the filename.

Yes, you can save the model and drawing docs through the API and assign new names. It works like the save-as. The original documents retain their original names. Prompt the user with a textbox, for a part name or input of your choice. Then use that to save the document. Make sure you specify the entire path along with the name. Not sure is this is what you're looking for. I have the code at work if you need it. Look at ModelDoc2.SaveAsSilent(newName As String, saveAsCopy As Boolean) As Long

T> > Just guessing, but would it be possible to remove the standard

This is the final solution we made for the problem (thanks to the SolidWorks API Support):

Dim swApp As SldWorks.SldWorks Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim FeatureData As Object Dim Feature As Object Dim Component As Object Dim TimeStamp As String Dim FileExtension As String Dim FileName As String

Private Declare Function GetSaveFileName _ Lib "COMDLG32.DLL" Alias "GetSaveFileNameA" _ (pOpenfilename As SAVEFILENAME) As Long

Private Type SAVEFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String Flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type

Sub Main()

Dim swModel As SldWorks.ModelDoc2 Dim strInitialFileName As String Dim strFileName As String Dim OFN As SAVEFILENAME Dim strInitialName As String

Set swApp =3D Application.SldWorks Set swModel =3D swApp.ActiveDoc

TimeStamp =3D Format(Now, "HHmmss") 'Timestamp o.b.v. uren, minuten, seconden TimeStamp =3D Right(TimeStamp, 4) 'Timestamp o.b.v. minuten, seconden strFileName =3D GetFileName(strInitialFileName, swModel.GetType) 'Filenaam cre=EBren FileName =3D "_" & TimeStamp & FileExtension 'timestamp en extensie combineren

With OFN

.lStructSize =3D Len(OFN) ' Size of structure. .nMaxFile =3D 260 ' Size of buffer. .lpstrInitialDir =3D "q:\" .lpstrFile =3D FileName .lpstrFilter =3D ("Part (*.prt;*.sldprt)") Ret =3D GetSaveFileName(OFN) ' Call function. If Ret 0 Then ' Non-zero is success.

Set swApp =3D Application.SldWorks Set Part =3D swApp.ActiveDoc Part.SaveAs2 FileName, 0, False, False

Else: MsgBox ("Bestandsnaam is leeg of er is op Cancel gedrukt!") End If End With

End Sub

Private Function GetFileName(ByVal strInitialName As String, ByVal nDocumentType As SwConst.swDocumentTypes_e) As String

' Initialize return value. GetFileName =3D ""

' Setup error handling. On Error GoTo FuncErr

' Set filter based on current document type. Select Case nDocumentType

Case swDocumentTypes_e.swDocPART

FileExtension =3D ".sldprt"

Case swDocumentTypes_e.swDocASSEMBLY

FileExtension =3D ".sldasm"

Case swDocumentTypes_e.swDocDRAWING

FileExtension =3D ".slddrw"

Case Else

' HERE: unsupported type.

' Bail out. Err.Raise 1

End Select

' Setup cancel handler. On Error GoTo CancelHandler

FuncExit:

' Release. Set cdFileDialog =3D Nothing

Exit Function

FuncErr:

Resume FuncExit

CancelHandler:

' HERE: user cancelled the dialog.

Resume FuncExit End Function

End Function

T> Yes, you can save the model and drawing docs through the API and assign n= ew

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required