我知道Stack Overflow上有类似的问题(和答案),但我特意试图提示最终用户保存文件并根据需要重命名,而不是自动创建文件。
我正在尝试创建一个宏,该宏将自动导出工作簿中的工作表,并以CSV格式保存非技术用户的文件。宏实际上工作正常,但它创建的每个文件都给出了错误消息"文件格式和扩展名为' NAME OF FILE.csv'不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它。你想打开吗?"如何在没有此警告消息的情况下打开此新文件?这是我的代码:
Sub copy_translated_file()
Dim DstFile As String 'Destination File Name
Dim wb As Workbook
Dim InitFile As String
InitFile = "BE SURE TO CHANGE THE NAME OF THIS FILE TO WHAT YOU WANT.csv"
Sheets("Translated").Select
Sheets("Translated").Copy
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Set wb = ActiveWorkbook
ActiveSheet.Buttons.Delete 'Get rid of the macro button
Columns("N:N").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
Range("F1").Select
DstFile = Application.GetSaveAsFilename(InitialFileName:=InitFile, _
fileFilter:="CSV (*.csv), *.csv", _
FilterIndex:=1, _
Title:="Save As")
If DstFile = "False" Then
MsgBox "Actions Canceled. File not saved."
wb.Close savechanges:=False 'Close File
Exit Sub
Else
wb.SaveAs DstFile 'Save file
wb.Close False 'Close file
MsgBox ("Translated file successfully saved in specified location.")
End If
End Sub
答案 0 :(得分:0)
这是Excel本身的设置,转到文件 - >选项 - >信托中心
另外,请确保您的标记设置为FileFormat:=xlCSV