[英]Can't save and close a XLS file in Access/VBA application correctly
我在 Access 和 VBA 方面很新,並試圖開發一個簡單的代碼:將表導出到 xls,打開它,簡單的操作(格式化),保存和關閉。
但在此過程中我收到以下消息框: "A file named RESUME.XLW" already exists in this location. Do you want to replace it?"
"A file named RESUME.XLW" already exists in this location. Do you want to replace it?"
選擇“是”將生成 xls 文件。 但是當我嘗試打開它時,Excel 以只讀模式運行,我不明白為什么。
我正在使用以下代碼:
Sub FormataExcelPadrao(caminhoExcel As String)
Set arquivoExcel = CreateObject("Excel.Application")
arquivoExcel.Workbooks.Open (caminhoExcel)
With arquivoExcel
For Each pagina In .Worksheets
With pagina
.Columns("A:Z").Autofit
.Cells.Font.Size = "10"
.Cells.Font.Name = "Calibri"
End With
Next pagina
End With
arquivoExcel.Save
arquivoExcel.Close
End Sub
提前致謝!
定義您的對象,然后使用它。 看這個例子
Sub FormataExcelPadrao(caminhoExcel As String)
Dim arquivoExcel As Object, wb As Object, pagina As Object
Set arquivoExcel = CreateObject("Excel.Application")
Set wb = arquivoExcel.Workbooks.Open(caminhoExcel)
With wb '<~~ Also this has to be the workbook and not excel app
For Each pagina In .Worksheets
With pagina
.Columns("A:Z").AutoFit
.Cells.Font.Size = "10"
.Cells.Font.Name = "Calibri"
End With
Next pagina
End With
wb.Close SaveChanges:=True
arquivoExcel.Quit
Set wb = Nothing
Set arquivoExcel = Nothing
End Sub
替換這個:
arquivoExcel.Save
和:
arquivoExcel.ActiveWorkbook.Save
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.