简体   繁体   English

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

[英]Can't save and close a XLS file in Access/VBA application correctly

I'm quite new in Access and VBA and trying to develop a simple code: export a table to a xls, open it, simple manipulations(formatting), save and close.我在 Access 和 VBA 方面很新,并试图开发一个简单的代码:将表导出到 xls,打开它,简单的操作(格式化),保存和关闭。

But I'm getting the following message box during the process: "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?" "A file named RESUME.XLW" already exists in this location. Do you want to replace it?"

Choosing "Yes" will generate the xls file.选择“是”将生成 xls 文件。 But when I try to open it, the Excel runs in read only mode, and I don't understanding why.但是当我尝试打开它时,Excel 以只读模式运行,我不明白为什么。

I'm using the following code:我正在使用以下代码:

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

Thanks in advance!提前致谢!

Define your objects and then use it.定义您的对象,然后使用它。 See this example看这个例子

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

Replace this:替换这个:

 arquivoExcel.Save

With:和:

 arquivoExcel.ActiveWorkbook.Save

See: http://www.tek-tips.com/viewthread.cfm?qid=1065376参见: http : //www.tek-tips.com/viewthread.cfm?qid=1065376

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM