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?"
"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!
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.