2

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.

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?"

Choosing "Yes" will generate the xls file. But when I try to open it, the Excel runs in read only mode, and I don't understanding why.

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!

4

2 に答える 2

6

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

Replace this:

 arquivoExcel.Save

With:

 arquivoExcel.ActiveWorkbook.Save

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

于 2013-01-11T21:19:24.470 に答える