为什么会这样?“activex component cant create object”
Me.CommonDialog1.CancelError = True
CommonDialog1.Filter = "(Excel)*.xls,*.xlsx|*.xls;*.xlsx"
Me.CommonDialog1.ShowOpen
sfn = Me.CommonDialog1.FileName
Text1.Text = sfn
If Err = cdlCancel Then
Exit Sub
End If
'判断文件是否已经打开
Dim excelApp As Object
shortName = fs.GetFileName(sfn)
Me.CommonDialog1.CancelError = True
CommonDialog1.Filter = "(Excel)*.xls,*.xlsx|*.xls;*.xlsx"
Me.CommonDialog1.ShowOpen
sfn = Me.CommonDialog1.FileName
Text1.Text = sfn
If Err = cdlCancel Then
Exit Sub
End If
'判断文件是否已经打开
Dim excelApp As Object
shortName = fs.GetFileName(sfn)
Set excelApp = GetObject(, "Excel.Application")
If Err Then
程序运行Set excelApp = GetObject(, "Excel.Application")就提示错误“activex component can‘t create object”
究竟是什么原因呢?请高手指点。
我的目的是想在打开文件前判断一下文件是否已经打开。
[解决办法]
Set excelApp = CreateObject( "Excel.Application")
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
GetObject必须要已经打开了Excel,而CreateObject的方式会不停打开新的Excel进程,最好的方式是
Dim excelApp As Object
on error resume next
Set excelApp = GetObject(, "Excel.Application") '先取已存在的Excel实例
on error goto 0
if excelApp is nothing then
Set excelApp = CreateObject( "Excel.Application") '没有就新开一个
end if
dim xlBook as object
on error resume next
set xlBook = GetObject("C:\Book1.xls", "Excel.Application")
on error goto 0
if xlBook is nothing then
msgbox "文件未打开"
else
msgbox "文件已打开"
end if