VB code for for activating the save dialog

Discussion in 'SolidWorks' started by hvdstroet, Sep 21, 2005.

  1. hvdstroet

    hvdstroet Guest

    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.
     
    hvdstroet, Sep 21, 2005
    #1
  2. 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).

    --


    Markku Lehtola

    www.markkulehtola.net
     
    Markku Lehtola, Sep 21, 2005
    #2
  3. hvdstroet

    TVO Guest

    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
     
    TVO, Sep 21, 2005
    #3
  4. hvdstroet

    hvdstroet Guest

    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:
     
    hvdstroet, Sep 21, 2005
    #4
  5. hvdstroet

    That70sTick Guest

    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.
     
    That70sTick, Sep 21, 2005
    #5
  6. hvdstroet

    mijo9a Guest

    yep, Not with the SW APIs but Win32 API
    I did it "long time ago" with sw03
    http://www.programming.e-cnc.com
    yes, you need vb6 and bit skills to subclass SW save as dialog

    Mike
     
    mijo9a, Sep 21, 2005
    #6
  7. hvdstroet

    hvdstroet Guest

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




    schreef:
     
    hvdstroet, Sep 22, 2005
    #7
  8. hvdstroet

    Matze Guest

    Unfortunately I need VB6 for this. And I don't have VB6.
    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.
     
    Matze, Sep 22, 2005
    #8
  9. hvdstroet

    hvdstroet Guest

    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.
     
    hvdstroet, Sep 23, 2005
    #9
  10. hvdstroet

    GreenHex Guest

    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.
    www.swCP3.com
     
    GreenHex, Sep 23, 2005
    #10
  11. 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.
     
    Markku Lehtola, Sep 23, 2005
    #11
  12. hvdstroet

    Tony Guest

    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

    Tony
     
    Tony, Sep 28, 2005
    #12
  13. hvdstroet

    hvdstroet Guest

    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 = Application.SldWorks
    Set swModel = swApp.ActiveDoc

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

    With OFN

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

    Set swApp = Application.SldWorks
    Set Part = 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 = ""

    ' Setup error handling.
    On Error GoTo FuncErr

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

    Case swDocumentTypes_e.swDocPART

    FileExtension = ".sldprt"

    Case swDocumentTypes_e.swDocASSEMBLY

    FileExtension = ".sldasm"

    Case swDocumentTypes_e.swDocDRAWING

    FileExtension = ".slddrw"

    Case Else

    ' HERE: unsupported type.

    ' Bail out.
    Err.Raise 1

    End Select

    ' Setup cancel handler.
    On Error GoTo CancelHandler


    FuncExit:

    ' Release.
    Set cdFileDialog = Nothing

    Exit Function


    FuncErr:

    Resume FuncExit


    CancelHandler:

    ' HERE: user cancelled the dialog.

    Resume FuncExit
    End Function

    End Function




    Tony schreef:
     
    hvdstroet, Oct 11, 2005
    #13
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.