萌新求助,VB输出文件保存
需要将数据输出到Excel并另存,如何在最后保存前弹出选择保存路径的选项?Dim iExcel As Object
Dim iBook As Object
Dim iSheet As Object
Set iExcel = CreateObject("Excel.Application")
Set iBook = iExcel.Workbooks.Add
Set iSheet = iBook.Worksheets(1)
iSheet.cells(1, 1).Value = "a"
iSheet.cells(1, 2).Value = "b"
iSheet.cells(1, 3).Value = "c"
iBook.SaveAs "D:\Book1.xlsx"
iExcel.Quit
可以使用Application.FileDialog方法来弹出选择保存路径的选项,代码如下:
Dim iExcel As Object
Dim iBook As Object
Dim iSheet As Object
Dim filePath As String
Set iExcel = CreateObject("Excel.Application")
Set iBook = iExcel.Workbooks.Add
Set iSheet = iBook.Worksheets(1)
iSheet.Cells(1, 1).Value = "a"
iSheet.Cells(1, 2).Value = "b"
iSheet.Cells(1, 3).Value = "c"
With Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName = "D:\Book1.xlsx"
If .Show = -1 Then
filePath = .SelectedItems(1)
iBook.SaveAs filePath
End If
End With
iExcel.Quit
首先,使用With语句创建一个文件保存对话框。设置InitialFileName属性为默认保存路径,然后使用Show方法弹出对话框。如果用户点击了保存按钮,就获取选择的路径并保存文件,否则不执行保存操作。最后关闭Excel应用程序。 isdkz 发表于 2023-9-22 16:07
可以使用Application.FileDialog方法来弹出选择保存路径的选项,代码如下:
Dim iExcel As Object
with语句一开始就报错怎么办 isdkz 发表于 2023-9-22 16:07
可以使用Application.FileDialog方法来弹出选择保存路径的选项,代码如下:
Dim iExcel As Object
我试了下,这个在VBA中实用,在VB中要怎么改呢。 尝试了好多遍,把下面的语句删了就可以显示保存界面了。
iBook.SaveAs "D:\Book1.xlsx"
页:
[1]