disasterkid
disasterkid

Reputation: 7278

Open/Save File Dialog That Works with Windows 64-bit

My open/save dialog box that used to work on windows XP no longer works on my Windows 7 64-bit. I have heard that 'MSComDlg.CommonDialog' is not compatible with 64-bit. Here is my old code:

' Sub to show open/save dialog
SUB OpenSave (varOpenSaveInputBox, varOpenSaveType, varOpenSaveFilter)   
   ' Create object
   SET objComDlg32 = CreateObject("MSComDlg.CommonDialog")
   ' Set memory buffer
   objComDlg32.MaxFileSize = 260
   ' Set filter
   objComDlg32.Filter = varOpenSaveFilter
   ' Show dialog 
   IF varOpenSaveType = 0 Then
      objComDlg32.ShowOpen
   ELSE
      objComDlg32.ShowSave
   End IF    
   ' Get filename from dialog
   strOpenSave = objComDlg32.FileName
   ' Check IF dialog is cancelled
   IF strOpenSave <> vbNullString Then
      ' Set to variable
      objOpenSave.SetContent strOpenSave, TRUE
   End If
END SUB

I would really appreciate if you be more specific with your answer rather than "Use this!". DLL and OCX is not really my strong suit. Thanks.

Upvotes: 1

Views: 12551

Answers (3)

Antoni Gual Via
Antoni Gual Via

Reputation: 763

You can still use hta, it's short and still works in win 10. Can't filter or change the selector window caption. You can even adapt it for use in a batch file..

Function SelectFile()
 Set ows=createobject("wscript.shell")
 Set oExec=ows.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();"&_
 "new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);"&_
 "close();resizeTo(0,0);</script>"""):   
 SelectFile = oExec.StdOut.ReadLine 
 Set ows=Nothing
End Function

Upvotes: 0

Geert Bellekens
Geert Bellekens

Reputation: 13784

I'm using this code, which I found somewhere on the internet (possibly even on StackOverflow. I don't remember exactly)

Function ChooseFile (ByVal initialDir, filter)

    dim shel, fso, tempdir, tempfile, powershellfile, powershellOutputFile,psScript, textFile
    Set shell = CreateObject("WScript.Shell")

    Set fso = CreateObject("Scripting.FileSystemObject")

    tempDir = shell.ExpandEnvironmentStrings("%TEMP%")

    tempFile = tempDir & "\" & fso.GetTempName

    ' temporary powershell script file to be invoked
    powershellFile = tempFile & ".ps1"

    ' temporary file to store standard output from command
    powershellOutputFile = tempFile & ".txt"

    'if the filter is empty we use all files
    if len(filter) = 0 then
    filter = "All Files (*.*)|*.*"
    end if

    'input script
    psScript = psScript & "[System.Reflection.Assembly]::LoadWithPartialName(""System.windows.forms"") | Out-Null" & vbCRLF
    psScript = psScript & "$dlg = New-Object System.Windows.Forms.OpenFileDialog" & vbCRLF
    psScript = psScript & "$dlg.initialDirectory = """ &initialDir & """" & vbCRLF
    'psScript = psScript & "$dlg.filter = ""ZIP files|*.zip|Text Documents|*.txt|Shell Scripts|*.*sh|All Files|*.*""" & vbCRLF
    psScript = psScript & "$dlg.filter = """ & filter & """" & vbCRLF
    ' filter index 4 would show all files by default
    ' filter index 1 would should zip files by default
    psScript = psScript & "$dlg.FilterIndex = 1" & vbCRLF
    psScript = psScript & "$dlg.Title = ""Select a file""" & vbCRLF
    psScript = psScript & "$dlg.ShowHelp = $True" & vbCRLF
    psScript = psScript & "$dlg.ShowDialog() | Out-Null" & vbCRLF
    psScript = psScript & "Set-Content """ &powershellOutputFile & """ $dlg.FileName" & vbCRLF
    'MsgBox psScript

    Set textFile = fso.CreateTextFile(powershellFile, True)
    textFile.WriteLine(psScript)
    textFile.Close
    Set textFile = Nothing

    ' objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn]) 
    ' 0 Hide the window and activate another window.
    ' bWaitOnReturn set to TRUE - indicating script should wait for the program 
    ' to finish executing before continuing to the next statement

    Dim appCmd
    appCmd = "powershell -ExecutionPolicy unrestricted &'" & powershellFile & "'"
    'MsgBox appCmd
    shell.Run appCmd, 0, TRUE

    ' open file for reading, do not create if missing, using system default format
    Set textFile = fso.OpenTextFile(powershellOutputFile, 1, 0, -2)
    ChooseFile = textFile.ReadLine
    textFile.Close
    Set textFile = Nothing
    fso.DeleteFile(powershellFile)
    fso.DeleteFile(powershellOutputFile)

End Function

Upvotes: 0

Zaider
Zaider

Reputation: 2013

This is VBA but it may be enough to point you in the right direction. The 3 declares the type of dialog you wish to open. You can find that and more information here: http://msdn.microsoft.com/en-us/library/office/ff865284.aspx.

Sub FileSelect (Multi as Boolean)
' Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set Dlg = Access.Application.FileDialog(3)
With Dlg
    .Title = "Select the file you want to open"
    .AllowMultiSelect = Multi
    If .Show = -1 Then
        txtFilePath = .InitialFileName
    Else
        Exit Function
    End If
End With

FileSelect = Dlg.SelectedItems(1)
End Function

Upvotes: 0

Related Questions