VBA调用EXCEL!
有一个调用Excel的过程如下:subeee()
On Error Resume Next
dim ex as Object
dim sheet as object
set ex = GetOjbect(, "Excel.Application")
if Err0 then
set ex = CreateObject("Excel.Application")
Err.claer
end if
ex.WorkSheets("Sheet1").Activate
end sub
系统使用 2002、Excel 2003 sp2
过程执行到ex.WorkSheets("Sheet1").Activate一句时总是提示"对象变量或With块变量未设置"的错误,检查之后发现ex并未获得Excel的引用不知道为什么!
请大家帮帮忙找找原因和解决方法!
这是由于使用CreateObject("Excel.Application")运行Excel时,并没有默认的创建一个文档(Workbook),可以使用ex.Workbooks.Add创建。
可是我并不想创建新的工作簿,而是直接访问当前活动的工作簿中的“Sheet1”工作表!
我应该怎么做呢?
DimstrDestination As String
Dim xlApp As Excel.Application
Dim xlBook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application ''激活EXCEL应用程序
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '隐藏EXCEL应用程序窗口
Set xlBook = xlApp.Workbooks.Open(strDestination) '打开工作簿,strDestination为一个EXCEL报表文件
Set xlSheet = xlBook.Worksheets(1) '设定工作表
'现在就可以直接访问当前活动的工作簿中的“Sheet1”工作表
xlBook.Save'保存文件
xlApp.Quit'退出EXCEL
页:
[1]