在应用Excel Application 对象时,如何判断Excel是否下在运行,及如何得到当前应用?

redcoral 2000-12-28 09:27:00
小弟在做一个Excel报表处理的时候,因为无法判断当然Excel是否在运行,不知各们大哥哪位能帮助在下!
Dim xlSumApp As Excel.Application
Set xlSumApp = New Excel.Application
xlSumApp.Visible=False
If xlSumApp=Nothing Then

MsgBox "Excel Not Install!"
End If
上面程序是判断是否安装Excel,那么如何才能判断当前Excel是否在运行,如果已经运行,那么如何得到当前的应用,也就是如何Set xlSumApp=??????才能不再建立一个新应用,还有就是,如果对当前Excel已经打开的工作薄操作的话,比如保存,Excel会问你是否保存,如果加答“否”,就会退出,所建Excel进程不会关闭,不知如何操作?如答出会再加分,多谢!


...全文
471 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lonstar 2001-01-02
  • 打赏
  • 举报
回复
没有分的????
michaelpeng7799 2000-12-28
  • 打赏
  • 举报
回复
在应用程序退出最后加一个 END语句

dim myExcel as new excel.application
dim myWb as new excel.workbook
form_unload()
myexcel.quit
mywb.quit
set myexcel=nothing
set myWb=nothing
end
end sub
DimVar 2000-12-28
  • 打赏
  • 举报
回复
参考一下GetObject和CreateObject函数吧……
set xlSumApp = GetObject(,"Excel.Application")
if xlSumaPP is Nothing then
set xlSumApp = CreateObject("Excel.Application")
end if
以上代码是想当然而写的,不知道对不对
redcoral 2000-12-28
  • 打赏
  • 举报
回复
怎么没人回答呀!
redcoral 2000-12-28
  • 打赏
  • 举报
回复
To:win2000 and lonstar ,小弟在此多谢二位,问题已经解决!
redcoral 2000-12-28
  • 打赏
  • 举报
回复
多谢两位!
lonstar 2000-12-28
  • 打赏
  • 举报
回复
这是我编写的一个小软件的原代码,有很详细的说明.
增加一个LISTVIEW(LISTVIEW1)及timer(timer1)控件。
Option Explicit

Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Public applicationName As Object '运行的应用软件对象名
Public prevFile As String '当前应用软件打开的文件名
Dim itmX As ListItem 'ListView中的列数组
Dim startDate As Date '开始时间
Dim endDate As Date '结束时间
Public newSession As Boolean
Private Sub Form_Load()
newSession = True

End Sub
Private Sub Timer1_Timer()

Dim str As String
Dim doc As Object
On Error Resume Next
' Set applicationName = GetObject(, "Excel.Application") '取得当前运行的Excel对象
' Set doc = applicationName.ActiveWorkBook '当前Excel打开的工作簿文件
Set applicationName = GetObject(, "Word.Application") '取得当前运行的Word对象
Set doc = applicationName.ActiveDocument '当前Word的applicationName打开的文件
' Set applicationName = GetObject(, "AutoCAD.Application") '取得当前运行的AutoCAD对象
' Set doc = applicationName.ActiveDocument '当前AutoCAD打开的图挡文件
str = doc.FullName '当前applicationName打开的文件名(包括路径)
If str = "" Then '当前无上述对象,则用"无工作文件"命名
str = "无工作文件"
SetWindowPos hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
SetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
If prevFile = str Then '时间增加
Dim actDate As Date
actDate = Now '将系统当前的时间给变量actDate
itmX.SubItems(1) = DateDiff("s", startDate, actDate) '计算开始时间与当前时间的差
Exit Sub '退出子过程
End If '若当前文件改变
If prevFile <> "" Then
endDate = Now ' 将系统当前的时间给变量endDate
itmX.SubItems(3) = Time '"时间结束",返回系统当前的时间。
applicationName = 0
End If
prevFile = str

Set itmX = ListView1.ListItems.Add(, , str) '在ListView中另起一行
If (ListView1.ListItems.Count Mod 2) = 0 Then itmX.ForeColor = vbRed Else itmX.ForeColor = vbBlue
itmX.SubItems(2) = Time '"时间开始",返回系统当前的时间。
startDate = Now ' 将系统当前的时间给变量startDateEnd Sub
End Sub
windows2000 2000-12-28
  • 打赏
  • 举报
回复
试一下这个方法:
dim winhwnd as long
winhwnd= FindWindow(vbNullString, "Excel")
If winhwnd <> 0 Then
MsgBox "excel正在运行!", vbOKOnly
end if

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧