我搜索了一下这样做的方法。
我想打开“另存为”对话框,以便用户可以选择保存文件的位置。但是,我希望“保存类型”字段预设为“逗号分隔值文件(* .csv)”
问题是“过滤器”方法似乎不适用于“msoFileDialogSaveAs”。是否可以使用“Application.FileDialog(msoFileDialogSaveAs)”预设文件类型?
目前,如果我使用.csv扩展名保存文件,然后在excel中打开它,我会收到“您尝试打开的文件xxx.csv的格式与指定的格式不同文件扩展名... “消息。该文件可以正常工作。
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "xxx"
.AllowMultiSelect = False
.InitialFileName = "xxx.csv"
'.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
result = .Show
If (result <> 0) Then
' create file
FileName = Trim(.SelectedItems.Item(1))
fnum = FreeFile
Open FileName For Output As fnum
' Write the csv data from form record set
For Each fld In rs.Fields
str = str & fld.Name & ", "
Next
' Write header line
str = Left(str, Len(str) - 2) ' remove last semi colon and space
Print #fnum, str
str = ""
' Write each row of data
rs.MoveFirst
Do While Not rs.EOF
For i = 0 To 40
str = str & rs(i) & ", " ' write each field seperated by a semi colon
Next i
str = Left(str, Len(str) - 2) ' remove last semi colon and space
Print #fnum, str
str = ""
rs.MoveNext
Loop
' close file
Close #fnum
End If
End With
比你!
答案 0 :(得分:9)
像往常一样,但希望是一个更好的解决方案...
Public Function GetSaveFilename() As String
Dim Dialog As FileDialog: Set Dialog = Application.FileDialog(msoFileDialogSaveAs)
With Dialog
.InitialFileName = CurrentProject.Path & "\*.ext"
.FilterIndex = 2
.Title = "Save As"
If .Show <> 0 Then
GetSaveFilename = .SelectedItems(1)
End If
End With
End Function
如何运作?
众所周知,您可以不直接在msoFileDialogSaveAs上设置过滤器。但是,如果将InitialFileName设置为“* .ext”,则会强制执行该扩展。过滤器仍然会显示“所有文件”,但它不会显示文件,除非它们具有您提供的扩展名。
结果
如果你删除“* .ext”并只写“test”,例如生成的文件名将是“test.ext”,所以它实际上会强制扩展。
它并不完美,但它非常简单,无需借助API调用就可以获得所需的结果。
<强>注意事项强>
这仅适用于一次扩展,例如的 “* .csv”。如果您需要过滤多个扩展程序,例如然后你必须使用API调用。
答案 1 :(得分:5)
正如迈克所写,并从他提出的链接;要默认选择所需的过滤器,您可以:
Sub Main()
Debug.Print userFileSaveDialog("unicode", "*.txt")
End Sub
Function userFileSaveDialog(iFilter As String, iExtension As String)
With Application.FileDialog(msoFileDialogSaveAs)
Dim aFilterIndex As Long: aFilterIndex = 0&
For aFilterIndex = 1& To .Filters.Count
If (InStr(LCase(.Filters(aFilterIndex).Description), LCase(iFilter)) > 0) _
And (LCase(.Filters(aFilterIndex).Extensions) = LCase(iExtension)) Then
.FilterIndex = aFilterIndex
Exit For
End If
Next aFilterIndex
If CBool(.Show) Then
userFileSaveDialog = .SelectedItems(.SelectedItems.Count)
Else
End
End If
End With
End Function
答案 2 :(得分:2)
如上所述,FileDialog
帮助状态msoFileDialogSaveAs
不受支持。
当对话框卸载时,您可以在FileName
强制执行CSV扩展程序;
FileName = getCSVName(FileName)
...
Function getCSVName(fileName As String) As String
Dim pos As Long
pos = InStrRev(fileName, ".")
If (pos > 0) Then
fileName = Left$(fileName, pos - 1)
End If
getCSVName = fileName & ".CSV"
End Function
如果excel不喜欢您的CSV,请检查是否有任何值需要引用以逃避换行/“(http://stackoverflow.com/questions/566052/can-you-encode-cr-lf-在 - 进入 - CSV文件)
而不是这种模式;
For i = 0 To 40
str = str & rs(i) & ", " ' write each field seperated by a semi colon
Next i
str = Left(str, Len(str) - 2) ' remove last semi colon and space
你可以;
dim delimiter as string
...
For i = 0 To 40
str = str & delimiter & rs(i) ' write each field seperated by a semi colon
delimiter = ","
Next
答案 3 :(得分:0)
http://msdn.microsoft.com/en-us/library/office/aa219834(v=office.11).aspx
使用filterindex从默认列表中选择所需的扩展名(启动对话框并将列表倒计时到您的扩展名),或修改saveas过滤器集合,如msdn中链接的页面中所述。无法在filedialog实例中更改过滤器,只能在具有通过Application.FileDialog(msoFileDialogSaveAs)的文件过滤器对象之前进行更改。过滤器。然后它们可以在实例中使用。
答案 4 :(得分:0)
Dim FileDialogObj As FileDialog
'1.0 Open File Dialog
Set FileDialogObj = Application.FileDialog(msoFileDialogSaveAs)
With FileDialogObj
.InitialFileName = "C:\"
.Filters.Item 3 '****This is to set File Dialog Save As to CSV ******
.Title = "Save As"
.AllowMultiSelect = False
End With