[英]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.