Open File Dialog Box Available in SW?

Discussion in 'SolidWorks' started by What-a-Tool, Sep 29, 2004.

  1. What-a-Tool

    What-a-Tool Guest

    It appears SW doesn't have a File open Dialog box available in VBA. Is there
    a reference I need to add?

    I thought I could use the Excel GetOpenFileName Dialog (code follows)
    This works, but obviously I'm doing something wrong here, because I get a
    "Server Busy - Switch Apps or Retry" Dialog right after the GetOpenFileName
    closes (gives me the filename, but this dialog warning is annoying)
    Could someone tell me what I'm doing wrong here?

    Private Sub btnBrowse_Click()
    On Error GoTo Err_btnBrowse_Click
    Dim fl As File
    Dim eApp As New Excel.Application
    'Get the path to a file with the file open dialog box
    Set fl = fso.GetFile(eApp.GetOpenFilename)
    eApp.Quit
    Set eApp = Nothing
    txtDirectory.Value = fl.ParentFolder

    Err_btnBrowse_Click:
    'Do Nothing
    End Sub

    Thanks in Advance...

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Sep 29, 2004
    #1
  2. What-a-Tool

    rocheey Guest

    closes (gives me the filename, but this dialog warning is annoying)
    ^^^^^^^^^^^^^^ < WSH reference
    ^^^^^^^^^^^^^^ < WSH reference
    I cant tell FOR SURE, because the references are not listed in Code,
    but it looks as though you need to reference the Windows Scripting
    Host Library.

    This is slow, BTW, and I have pure VB/VBA source for File open,
    Multiple file open, file save, browse for folder, etc, uses all API
    calls, and works in both VB and VBA.

    I just dont feel like taking the time to add word wrap to all the
    lines of code so it somes thru Usenet clean. Email me and Ill send you
    the module....
     
    rocheey, Sep 29, 2004
    #2
  3. What-a-Tool

    That70sTick Guest

    The Windows Common Dialog object used for
    opening/saving/colors/priniting is not licensed for use in SW VBA. You
    have to access it using Windows API calls (Declare...etc.).

    I used to know how to do this. I forgot because now I have the full
    version of VB6 and everything is licensed. I have some old code that
    works, though.

    Below is code for accessing windows common dialog thrugh Windows API.
    I tincludes a "Declare" statement to access the function, a "Type"
    statement to set up a data type needed for the API call, and a wrapper
    function for opening SW files. Similar contortions are required for
    Save dialog.

    '==========================================
    'begin code
    '==========================================
    ' Windows API for the Open Filebox
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
    "GetOpenFileNameA" _
    (pOpenfilename As OPENFILENAME) As Long

    ' structure needed by Windows API
    Private Type OPENFILENAME
    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

    Private Function FileNameToOpen() As String
    ' common dialog for browse for desired filename

    'set variables for OPENFILENAME type
    Dim OFName As OPENFILENAME
    'Set the filter
    OFName.lpstrFilter = "SW files (*.sld*)" + Chr$(0) + "*.sld*" +
    Chr$(0) + "SW part files (*.sldprt)" + Chr$(0) + "*.sldprt" + Chr$(0) +
    "SW assembly files (*.sldasm)" + Chr$(0) + "*.sldasm" + Chr$(0) + "SW
    drawing files (*.slddrw)" + Chr$(0) + "*.slddrw" + Chr$(0)
    'default extension
    OFName.lpstrDefExt = "" + Chr$(0)
    'Set the initial directory
    'OFName.lpstrInitialDir = ""
    OFName.lpstrInitialDir = "C:\"
    'Set the dialog title
    OFName.lpstrTitle = "Select SolidWorks file"

    'Set the structure size
    OFName.lStructSize = Len(OFName)
    'Create a buffer
    OFName.lpstrFile = Space$(254)
    'Set the maximum number of chars
    OFName.nMaxFile = 255
    'Create a buffer
    OFName.lpstrFileTitle = Space$(254)
    'Set the maximum number of chars
    OFName.nMaxFileTitle = 255
    'no extra flags
    OFName.flags = 0

    'Show the 'Open File'-dialog
    If GetOpenFileName(OFName) Then
    FileNameToOpen = Trim$(OFName.lpstrFile)
    Else
    FileNameToOpen = ""
    End If

    End Function
     
    That70sTick, Sep 30, 2004
    #3
  4. What-a-Tool

    rocheey Guest

    'Set the filter

    .... ohhh, didnt have the str$ to set the file type list .. SNIP!!
     
    rocheey, Sep 30, 2004
    #4
  5. What-a-Tool

    What-a-Tool Guest

    Very interesting .... Thanks a bunch

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Oct 2, 2004
    #5
  6. What-a-Tool

    What-a-Tool Guest

    Thanks a bunch - used your code, works great - I'm opening files all over
    the place, now!!!

    --

    / Sean the Mc /


    "I have not failed. I've just found 10,000 ways that won't work."
    - Thomas Alva Edison (1847-1931)
     
    What-a-Tool, Oct 5, 2004
    #6
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.