2

我是 Access 和 VBA 的新手,并试图开发一个简单的代码:将表导出到 xls,打开它,简单的操作(格式化),保存并关闭。

但在此过程中我收到以下消息框:"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

提前致谢!

4

2 回答 2

6

定义您的对象,然后使用它。看这个例子

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
于 2013-01-11T21:42:23.607 回答
0

替换这个:

 arquivoExcel.Save

和:

 arquivoExcel.ActiveWorkbook.Save

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

于 2013-01-11T21:19:24.470 回答