关于VBA中excel进程关闭问题!

cactus_camel 2006-11-10 09:09:49
在程序中,我通过createobject来创建了一个excel对象,并进行了相关的操作后,我想去关闭该excel方法如下:
dim xlsapp as excel.application
dim xlsbk as excel.workbook
dim xlsst as excel.worksheet
....................(相关操作后)
xlsst=nothing
xlsbk.close()
xlsbk=nothing
xlsapp.application.quit()
xlsapp=nothing
这些代码好像只能在我的程序整个退出时,excel才跟着退出!如果我的主程序没有结束的话,上面的代码只是把excel的界面给销毁,但是在进程里还是存在excel。不知道在自己的程序里面如何能结束excel进程,而不只是销毁窗口。
...全文
1835 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cactus_camel 2006-11-12
  • 打赏
  • 举报
回复
十分感谢大家!我会尝试你们给的建议!给分!!
水如烟 2006-11-11
  • 打赏
  • 举报
回复
它只关闭自己的Excel进程.

首先尝试关闭,gExcelApplication.Quit()
第二次尝试关闭,System.Runtime.InteropServices.Marshal.ReleaseComObject(gExcelApplication)
两种方法还不行的话,杀自己的Excel进程,
LzmTW.uSystem.uDiagnostics.uProcess.ProcessServices.Kill("EXCEL", gBeforeProcessStartTime, gAfterProcessStartTime)
水如烟 2006-11-11
  • 打赏
  • 举报
回复
Namespace uSystem.uDiagnostics.uProcess
Public Class ProcessServices
Private Sub New()
End Sub


''' <summary>
''' 停止进程
''' </summary>
''' <param name="processName">进程名称</param>
''' <param name="beforeStartTime">进程启动前的时间</param>
''' <param name="afterStartTime">里程启动后的时间</param>
''' <remarks></remarks>
Public Shared Sub Kill(ByVal processName As String, ByVal beforeStartTime As DateTime, ByVal afterStartTime As DateTime)

Dim mProcessList As Process()
Dim mProcessStartTime As DateTime

mProcessList = Process.GetProcessesByName(processName)

For Each tmpProcess As Process In mProcessList
mProcessStartTime = tmpProcess.StartTime
If mProcessStartTime.CompareTo(beforeStartTime) > 0 AndAlso mProcessStartTime.CompareTo(afterStartTime) < 0 Then
tmpProcess.Kill()
End If
Next

End Sub

''' <summary>
''' 停止进程
''' </summary>
''' <param name="processName">进程名称</param>
''' <remarks></remarks>
Public Shared Sub Kill(ByVal processName As String)

Dim mProcessList As Process()

mProcessList = Process.GetProcessesByName(processName)

For Each tmpProcess As Process In mProcessList
tmpProcess.Kill()
Next

End Sub
End Class
End Namespace

水如烟 2006-11-11
  • 打赏
  • 举报
回复
仅供参考:

Option Strict Off

Namespace NET
Friend Class ExcelQueryTable
Private gExcelApplication As Object
Private gWorkbook As Object
Private gWorksheet As Object
Private gQueryTable As Object

Private gConnectionString As String = "URL;{0}"

'以下参数使当前ExcelApplication完全退出
Private gBeforeProcessStartTime As DateTime
Private gAfterProcessStartTime As DateTime

Sub New()
Initialize()
End Sub

Private Sub Initialize()
'登记进程生成的前后时间
gBeforeProcessStartTime = Now
gExcelApplication = CreateObject("Excel.Application")
gAfterProcessStartTime = Now

gExcelApplication.DisplayAlerts = False '使退出时不询问是否存盘
gWorkbook = gExcelApplication.Workbooks.Add
gWorksheet = gWorkbook.Worksheets.Add

gQueryTable = gWorksheet.QueryTables.Add( _
Connection:="URL;", _
Destination:=gWorksheet.Range("A1"))
End Sub

Public Sub Close()

Try
gQueryTable.Delete()
System.Runtime.InteropServices.Marshal.ReleaseComObject(gQueryTable)
gQueryTable = Nothing

gWorkbook.Close()

System.Runtime.InteropServices.Marshal.ReleaseComObject(gWorksheet)
gWorksheet = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(gWorkbook)
gWorkbook = Nothing

gExcelApplication.DisplayAlerts = True
gExcelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(gExcelApplication)
gExcelApplication = Nothing

System.Threading.Thread.Sleep(500)
Catch ex As Exception

End Try

LzmTW.uSystem.uDiagnostics.uProcess.ProcessServices.Kill("EXCEL", gBeforeProcessStartTime, gAfterProcessStartTime)
End Sub

''' <summary>
''' 下载网上区划码数据至Datatable
''' </summary>
''' <param name="address">数据所在网页地址</param>
''' <param name="webtableIndex">对应Excel中QueryTable的WebTable参数</param>
''' <param name="outputTable">数据输出所在的DataTable</param>
''' <remarks>outputTable有两列,列名分别为Code,Name;不适合多线程</remarks>
Public Sub Query(ByVal address As String, ByVal webtableIndex As Integer, ByVal outputTable As DataTable)

gWorksheet.Cells.Clear()

With gQueryTable
.Connection = String.Format(gConnectionString, address)
.WebTables = webtableIndex
.Refresh(BackgroundQuery:=False)
End With


Dim mCell As Object
Dim mMaxRowIndex As Integer
Dim line As Object

mMaxRowIndex = gWorksheet.Cells.SpecialCells(11).Row 'Excel.XlCellType.xlCellTypeLastCell=11
mCell = gWorksheet.Range("A1")

For i As Integer = 0 To mMaxRowIndex
line = mCell.Offset(i, 0).Value
If line IsNot Nothing Then
AddRow(outputTable, line.ToString)
End If
Next

End Sub

Private Sub AddRow(ByVal table As DataTable, ByVal line As String)
line = line.Trim
If line.Length < 7 Then Exit Sub

Dim tmpCode As String
Dim tmpName As String

tmpCode = line.Substring(0, 6)
tmpName = line.Substring(6).Trim

If Not IsNumeric(tmpCode) Then Exit Sub '前六位需是数字

'去掉名称中间的空格
table.Rows.Add(New String() {tmpCode, tmpName.Replace(" ", "")})
End Sub

End Class
End Namespace
cactus_camel 2006-11-11
  • 打赏
  • 举报
回复
谢谢,楼上的建议,不过除了这种办法EXCEL本身没有提供相应的函数来终结吗?就象我们自己打开一个excel程序,通过关闭菜单就能关闭一样!
sx_lxh 2006-11-11
  • 打赏
  • 举报
回复
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try{
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}
}
}
catch(Exception Exc)
{
msg.Text+= "杀死" + processName + "失败!";
}
}
hei__an 2006-11-11
  • 打赏
  • 举报
回复
MARK

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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