我遇到的问题是文档有时会很好地导入,而有时则不会。从外观上看,Excel 并不总是关闭。谁能建议一种方法来确保打开的 Excel 实例已关闭?或者为我指出一个很好的参考?我在 MS Access 中使用这个 VBA。下面是我的代码:
Public Function ImportPayment()
Dim fd As FileDialog
Dim wb As Workbook
Dim ws As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select a file."
.Show
On Error GoTo ErrorHandler:
ws = fd.SelectedItems(1)
Workbooks.Open (ws)
Rows("1:9").Select
Selection.Delete Shift:=xlUp
Cells(Rows.count, "L").End(xlUp).EntireRow.Delete
Excel.Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:= _
"\\servername\serverfolder\serversubfolder\subfolder\sub\file.txt",
FileFormat:=xlText _ , CreateBackup:=False
End With
ActiveWorkbook.Close
Excel.Application.DisplayAlerts = True
Excel.Application.Quit
DoCmd.TransferText acImport, "Specification", "table",
"\\server\serverfolder\serversubfolder\subfolder\sub\file.txt", False
MsgBox ("Your data was imported successfully.")
ImportPayment = Yep
Exit Function
ErrorHandler:
ImportPayment = Nope
MsgBox ("The upload was canceled.")
End Function