Author Topic: open dialog window in the current folder  (Read 2475 times)

0 Members and 1 Guest are viewing this topic.

montzagcg

  • Guest
open dialog window in the current folder
« on: December 10, 2008, 03:05:22 AM »
Hi,

I need some help. I have 2 radio buttons, when I press one of them in the list that is assigned it shows all my dwg from the current folder. I've put it a browse button so I can open a window and to change the curent folder but I don't know how to update the path for radio button and the open dialog window to be in the same folder. Can anyone help me?

Thank you.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: open dialog window in the current folder
« Reply #1 on: December 10, 2008, 08:22:50 AM »
It sounds like you need a GLOBAL variable for the folder path.  When you change the path from one button it will automatically be updated for the other.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: open dialog window in the current folder
« Reply #2 on: December 10, 2008, 08:35:11 AM »
It depends upon how you are opening the browse window .. some code on how you handle this would be very helpful ... to update the radio button, all you need to do is change the caption property of the button.

Do you need the browse button? If not, you could simply use the dir function to return all the DWG files located in a specific path as such:

Code: [Select]
PlotFile = Dir(Folder & "\*.dwg")
While PlotFile <> ""
ListBox.AddItem PlotFile
PlotFile = Dir
Wend
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

montzagcg

  • Guest
Re: open dialog window in the current folder
« Reply #3 on: December 11, 2008, 02:01:30 AM »

1. I have the global variable but I don't know to pass for the open dialog window. To open the dialog window I used this command: ThisDrawing.SendCommand ("open" & vbCr). This command is open a window like you want to open from menu: File - Open...


2. The part with the refresh of radio button is done, also the list it's ok. I need the browse button so I can open a dialog window for changing the folder and to add or select new drawings. Now For me the only problem is to open the dialog window in the current folder. I've tried also with this:

Code: [Select]
Sub open_project_window()
Dim RetVal
Dim dwg_path As String
Dim PathTokens As Variant
Dim ProjectPath As String

dwg_path = ThisDrawing.Path
PathTokens = Split(dwg_path, "\")
ProjectPath = PathTokens(0) & "\" & PathTokens(1) & "\" & PathTokens(2) & "\" & PathTokens(3)

[color=red]RetVal = Shell("C:\Windows\Explorer.exe " & ProjectPath, 1) - I don't want to open an explorer window[/color]

End Sub

This code it's ok, but I don't want to open the Explorer window, any ideas?

rogue

  • Guest
Re: open dialog window in the current folder
« Reply #4 on: December 11, 2008, 01:20:37 PM »

 I need the browse button so I can open a dialog window
...
This code it's ok, but I don't want to open the Explorer window, any ideas?

Well, the dirty truth is, whether you use Shell, or call a file open dialog, its ALL explorer.

But anyhoo, the quickest and dirtiest way that *might* work for you, is using "ChDir". I dont know exactly how you are generating your dialog, but sometimes setting the current windows directory works in those situations.

or, if you wanna do it RIGHT ...

Drop the following code into its own (.bas) module.
It contains "File Open" and "Save File" dialogs. It doesnt actually open or save the files for you, but it offers the users an interface, and it returns the selected file (or returns an empty string if the user cancels)

I also added the "SetCurrentDirectory" function, which seeds the starting window for the dialogs. This API version works better than the "ChDir" function of VB, because it also works with network drives, etc.

Some sample calls:
Code: [Select]
Sub main()

    Dim retFile As String
   
    ' set directory you want  dialog to start in as the \windows directory
    SetCurrentDirectory "c:\windows"
   
   
    ' call File Open Dialog
    FileOpen Application.hWnd, MyFile$, "*.dwg", "Acad Drawings", "Select a  Drawing"
    MsgBox "The File selected to open was " & MyFile$
   
    ' set directory you want  dialog to start in as the root dir
    SetCurrentDirectory "c:\"
   
    ' call file save dialog
    SaveFile Application.hWnd, MyFile$, "*.txt", "Text Files", "Save a text file"
    MsgBox "The File selected to save was " & MyFile$


End Sub


' and the attached code module:
Code: [Select]

Option Explicit


Public Const OFN_ALLOWMULTISELECT = &H200
Public Const OFN_CREATEPROMPT = &H2000
Public Const OFN_ENABLEHOOK = &H20
Public Const OFN_ENABLETEMPLATE = &H40
Public Const OFN_ENABLETEMPLATEHANDLE = &H80
Public Const OFN_EXPLORER = &H80000
Public Const OFN_EXTENSIONDIFFERENT = &H400
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_LONGNAMES = &H200000
Public Const OFN_NOCHANGEDIR = &H8
Public Const OFN_NODEREFERENCELINKS = &H100000
Public Const OFN_NOLONGNAMES = &H40000
Public Const OFN_NONETWORKBUTTON = &H20000
Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_NOTESTFILECREATE = &H10000
Public Const OFN_NOVALIDATE = &H100
Public Const OFN_OVERWRITEPROMPT = &H2
Public Const OFN_PATHMUSTEXIST = &H800
Public Const OFN_READONLY = &H1
Public Const OFN_SHAREAWARE = &H4000
Public Const OFN_SHAREFALLTHROUGH = 2
Public Const OFN_SHAREWARN = 0
Public Const OFN_SHARENOWARN = 1
Public Const OFN_SHOWHELP = &H10
Public Const OFS_MAXPATHNAME = 128
 
Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS
Public Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY
Public Const OFS_MULTIFILE_OPEN_FLAGS = OFN_ALLOWMULTISELECT Or OFN_EXPLORER Or OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS

Public Type OPENFILENAME
    nStructSize As Long
    hwndOwner As Long
    hInstance As Long
    sFilter As String
    sCustomFilter As String
    nCustFilterSize As Long
    nFilterIndex As Long
    sFile As String
    nFileSize As Long
    sFileTitle As String
    nTitleSize As Long
    sInitDir As String
    sDlgTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExt As Integer
    sDefFileExt As String
    nCustDataSize As Long
    fnHook As Long
    sTemplateName As String
End Type
 
Public Llama As OPENFILENAME
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long






'  +--------------------------------------------------------------------+
'  |             -= Main sub to call File SAVE Dialog =-                |
'  |                                                                    |
'  | Parameters: FileName$ is a variable that the name of the SAVED     |
'  |            file name is returned in. You do NOT have to pass       |
'  |            a filename to this routine, one is returned. Note       |
'  |            that the Win API checks for, and prompts, if the        |
'  |            filename already exists.                                |
'  |                                                                    |
'  |            FileExt$ is the file extension name you wish the        |
'  |            Dialog box to use, for default extension, file          |
'  |            listings, and availablity innthe drop-down "file        |
'  |            type" box.                                              |
'  |                                                                    |
'  |            FileDesc$ is a descriptive name for the File Name       |
'  |            Extension, used to describe the filetype in the drop    |
'  |            down type box.                                          |
'  |                                                                    |
'  |            DlgTitle$ is the name of the caption on the Dialog      |
'  |                                                                    |
'  |                                                                    |
'  +--------------------------------------------------------------------+
Public Sub SaveFile(hWnd As Long, FileName$, FileExt$, FileDesc$, DlgTitle$)

  Dim lngGo As Long
  Dim lngHwnd As Long
  Dim strCurName As String
  Dim strNewName As String

    On Error GoTo Err_Control
    strCurName = FileName$
    lngHwnd = hWnd
    FileName$ = vbdShowSave(lngHwnd, strCurName, FileExt$, FileDesc$, DlgTitle$)

    Exit Sub
Err_Control:
    'Just get out, to many things to account for
    MsgBox Err.Description, vbCritical, "Too many errors, aborting"
End Sub


'  +--------------------------------------------------------------------+
'  |             -= Main sub to call File OPEN Dialog =-                |
'  |                                                                    |
'  | Parameters: FileName$ is a variable that the name of the SAVED     |
'  |            file name is returned in. You do NOT have to pass       |
'  |            a filename to this routine, one is returned.            |
'  |                                                                    |
'  |            FileExt$ is the file extension name you wish the        |
'  |            Dialog box to use, for default extension, file          |
'  |            listings, and availablity innthe drop-down "file        |
'  |            type" box.                                              |
'  |                                                                    |
'  |            FileDesc$ is a descriptive name for the File Name       |
'  |            Extension, used to describe the filetype in the drop    |
'  |            down type box.                                          |
'  |                                                                    |
'  |                                                                    |
'  |            DlgTitle$ is the name of the caption on the Dialog      |
'  |                                                                    |
'  |                                                                    |
'  +--------------------------------------------------------------------+
Public Sub FileOpen(hWnd As Long, FileName$, FileExt$, FileDesc$, DlgTitle$)
   
    Dim lngGo As Long
    Dim lngHwnd As Long
    Dim strCurName As String
    Dim strNewName As String

    On Error GoTo Err_Control
    strCurName = FileName$

     lngHwnd = hWnd
    strNewName = vbdShowOpen(lngHwnd, strCurName, FileExt$, FileDesc$, DlgTitle$)
    FileName$ = strNewName

Exit Sub
Err_Control:
    'Just get out, to many things to account for
    MsgBox Err.Description, vbCritical, "Too many errors, aborting"
End Sub

'   +---------------------------------------------------------------+
'   |   Interface from the "OpenFile" routine to the Windows API     |
'   +---------------------------------------------------------------+
Public Function vbdShowOpen(lngHwnd As Long, strDwgName As String, FileExt$, FileDesc$, DlgTitle$) As Variant

 Dim lngReturn As Long, ShortSize As Long
 Dim LongName As String, shortName As String, strFill As String
 Dim strDblSpace As String, strFilter As String

    strFill = Chr(0): strDblSpace = strFill & strFill
    Llama.nStructSize = Len(Llama)
    Llama.hwndOwner = lngHwnd

    'This section is for the filter drop down list
      strFilter = FileDesc$ & strFill & FileExt$ & strFill
      strFilter = strFilter & "All Files" & strFill & "*.*" & strDblSpace
      Llama.sFilter = strFilter
    'This is the default information for the dialog
      Llama.sFile = strDwgName & Space$(1024) & strFill
      Llama.nFileSize = Len(Llama.sFile)
      Llama.sDefFileExt = FileExt$
     
      Llama.sFileTitle = Space(512)
      Llama.nTitleSize = Len(Llama.sFileTitle)
      Llama.sInitDir = CurDir
      Llama.sDlgTitle = DlgTitle$
     
    ' use below to call open dialog
      Llama.flags = OFS_FILE_OPEN_FLAGS
      lngReturn = GetOpenFileName(Llama)
   
      If lngReturn Then
         vbdShowOpen = Llama.sFile
      End If

End Function


'   +---------------------------------------------------------------+
'   |   Interface from the "SaveFile" routine to the Windows API     |
'   +---------------------------------------------------------------+
Public Function vbdShowSave(lngHwnd As Long, strDwgName As String, FileExt$, FileDesc$, Caption$) As String

  Dim lngReturn As Long, ShortSize As Long
  Dim LongName As String, shortName As String
  Dim strFill As String, strDblSpace As String, strFilter As String

  strFill = Chr(0): strDblSpace = strFill & strFill
  Llama.nStructSize = Len(Llama)
  Llama.hwndOwner = lngHwnd
   
  'This section is for the filter drop down list
    strFilter = FileDesc$ & strFill & FileExt$ & strFill
    strFilter = strFilter & "All Files" & strFill & "*.*" & strDblSpace
    Llama.sFilter = strFilter
  'This is the default information for the dialog
    Llama.sFile = strDwgName & Space$(1024) & strFill
    Llama.nFileSize = Len(Llama.sFile)
    Llama.sDefFileExt = FileExt$
     
    Llama.sFileTitle = Space(512)
    Llama.nTitleSize = Len(Llama.sFileTitle)
    Llama.sInitDir = CurDir
    Llama.sDlgTitle = Caption$
     
  ' use below to call save dialog
    Llama.flags = OFS_FILE_SAVE_FLAGS
    lngReturn = GetSaveFileName(Llama)
   
    If lngReturn Then
      vbdShowSave = Llama.sFile
    End If

End Function