2013-01-11 41 views
2

我在Access和VBA中很新,嘗試開發一個簡單的代碼:將表導出到xls,打開它,進行簡單的操作(格式化) , 保存並關閉。無法在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 

提前感謝!

回答

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 
+1

謝謝@TimWilliams鄧諾我怎麼錯過了。更新!下一次,如果你看到任何這樣的愚蠢的錯誤,隨時修改它:) –

+0

工程罰款我的朋友!非常感謝! ;) –

相關問題