从Access打开电子表格和添加VBAComponent的问题

时间:2017-09-21 20:54:14

标签: excel ms-access

我正在尝试在Microsoft Access中组合一个函数,该函数将打开电子表格,添加VBComponent,保存电子表格,并完全关闭。下面的代码似乎几乎可以工作,但完成后不保存更改。有人可以告诉我需要更改什么才能让电子表格成功保存吗?

Sub AddSht_AddCode()
Dim wb As Workbook
Dim xPro As VBIDE.VBProject
Dim xCom As VBIDE.VBComponent
Dim xMod As VBIDE.CodeModule
Dim xLine As Long


Set wb = Workbooks.Open("C:\Users\U181873\Desktop\Book1.xlsx")

With wb
    Set xPro = .VBProject
    Set xCom = xPro.VBComponents("Sheet1")
    Set xMod = xCom.CodeModule

    With xMod
        xLine = .CreateEventProc("Change", "Worksheet")
        xLine = xLine + 1
        .InsertLines xLine, "  Cells.Columns.AutoFit"
    End With
wb.Save
wb.Close
End With
End Sub

0 个答案:

没有答案