如何在VBA中的Access 2007中编辑excel电子表格后如何保存它

时间:2014-02-19 03:27:15

标签: excel vba ms-access-2007 access-vba

我正在尝试让Access 2007打开Excel电子表格,将数据添加到单元格,然后保存电子表格。我在Access 2007表单中有一个按钮,可以激活以下代码。这是我到目前为止所拥有的,但每个“保存命令”都不起作用。有没有人知道该用什么?

Private Sub buttonExcel_Click()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    ' Create a New Excel WorkBook
    Set oExcel = CreateObject("Excel.Application")

    ' Optional, Open a current Workbook from a file directory
    oExcel.Workbooks.Open ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx")

    ' Make the Excel Workbook visible to the users
    oExcel.Visible = True

    ' Define the Workbook from Excel
    Set oBook = oExcel.ActiveWorkbook

    ' Define the Workskeet from the Workbook (1="Sheet1", 2="Sheet2", etc...)
    Set oSheet = oBook.Worksheets(1)

    ' Write Data to the Worksheet (Block "A1" is the first row, first column)
    oSheet.Range("A1").Value = "Hello World"

    ' Save the Excel Workbook
    oExcel.SaveAs ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx") ' DOESNT WORK
    oExcel("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx").Save ' DOESNT WORK
    oBook.SaveAs ("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx") ' DOESNT WORK
    oBook("Z:\08_Volume Management\ACCESS\EMAILTEMPLATES\test.xlsx").Save ' DOESNT WORK

    ' Closes Excel as asks to save the Workbook
    oExcel.Workbooks.Close

    ' Quit Excel
    'oExcel.Quit
End Sub

感谢名单!

1 个答案:

答案 0 :(得分:0)

Private Sub buttonExcel_Click()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    Set oExcel = CreateObject("Excel.Application")

    Set oBook = oExcel.Workbooks.Open ("Z:\08_Volume Management\" & _
                                 "ACCESS\EMAILTEMPLATES\test.xlsx")
    oExcel.Visible = True

    Set oSheet = oBook.Worksheets(1)

    oSheet.Range("A1").Value = "Hello World"

    oBook.Save
    oBook.Close
    'or...
    'oBook.Close True 'True = save changes

    oExcel.Quit

End Sub