【问题标题】:Open/Save File Dialog That Works with Windows 64-bit适用于 Windows 64 位的打开/保存文件对话框
【发布时间】:2012-11-27 21:01:24
【问题描述】:

以前在 Windows XP 上运行的打开/保存对话框在我的 64 位 Windows 7 上不再运行。我听说“MSComDlg.CommonDialog”与 64 位不兼容。这是我的旧代码:

' 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

如果您的回答比“使用这个!”更具体,我将不胜感激。 DLL 和 OCX 并不是我真正的强项。谢谢。

【问题讨论】:

  • “不再有效”到底是什么意思?我觉得这不是Win7 64位的问题。你有什么office版本?你必须有开发者版。
  • 什么语言? vba、vbscript 和 activex 都是不同的技术
  • @DanielDusek 我不在办公室使用它,而是在 QlikView 中使用它。我需要一个在 64 位 win7 中工作的 openfile 对话。如果您知道 'MSComDlg.CommonDialog 的替代品,我将不胜感激。
  • @Sean Cheshire vbscript.
  • 这是一个很老的问题,你可能已经得到了答案,但对于那些可能已经在寻找相同答案的人来说——唯一的“通用”方法是使用 Shell.Application 中的 BrowseForFolder 方法( msdn.microsoft.com/en-us/library/windows/desktop/…)

标签: vbscript openfiledialog


【解决方案1】:

这是 VBA,但它可能足以为您指明正确的方向。 3 声明您希望打开的对话框类型。你可以在这里找到更多信息: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

【讨论】:

    【解决方案2】:

    我正在使用这段代码,我在互联网上的某个地方找到了它(甚至可能在 StackOverflow 上。我不记得具体了)

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多