在access 中打开excel,先要添加对excel的引用。
代码如下:
Sub myt()
Dim xl As excel.Workbooks
excel.Application.Visible = True
Set xl = excel.Workbooks
xl.Open "D:/workfile/MWbalance/mwbalance.xls"
End Sub
但是这样打开的excel在另一个窗口,并且没有宏。解决方法有两个。
1 最简单的是用 ShellExcecute (windows API)
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Function OpenExcel()
ShellExecute 0, "open", "D:/workfile/MWbalance/mwbalance.xls", 0, 0, 0
End Function
2 在当前excel窗口中打开
Function OpenExcel()
Dim xl As excel.Workbook
Set xl = CreateObject("Excel.Sheet")
xl.Application.Workbooks.Open "D:/workfile/MWbalance/mwbalance.xls"
Set xl = Nothing
End Function