gpt4 book ai didi

excel - 无法在 Access/VBA 应用程序中正确保存和关闭 XLS 文件

转载 作者:行者123 更新时间:2023-12-03 00:32:06 27 4
gpt4 key购买 nike

我对 Access 和 VBA 很陌生,正在尝试开发一个简单的代码:将表导出到 xls,打开它,简单的操作(格式化),保存并关闭。

但在此过程中我收到以下消息框:“此位置已存在名为 RESUME.XLW 的文件”。你想更换它吗?”

选择"is"将生成 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

关于excel - 无法在 Access/VBA 应用程序中正确保存和关闭 XLS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14286658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com