设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1154|回复: 4
打印 上一主题 下一主题

[窗体] access如何关闭系统进程中的word程序?

[复制链接]
跳转到指定楼层
1#
发表于 2010-1-14 20:41:03 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
我的access数据库因为要传送数据到word表中,在调试中,常出现要打开的word文档已经打开,或者word域填充出错之类的错误。这时我必须按CTRL+ALT+DEL打开系统进程,在进程中关闭word程序,否则我的数据库无法继续测试和进行下一步的操作。

因此我想请教,如何关闭系统进程中错误的word进程呢?

我曾编写的错误处理代码如下:

On Error GoTo Err_cmdenter_Click

'Dim n As New Word.Application
'Dim wordDoc As Word.Document

……
……

Exit_cmdenter_Click:
    Exit Sub
Err_cmdenter_Click:
    MsgBox "填充Word域错误," & Err.Description, vbCritical, "出错"
   
    wordDoc.Close   

    Set wordDoc = Nothing
   
    n.Quit
    Set n = Nothing
     
    DoCmd.Hourglass False
    Resume Exit_cmdenter_Click

但该代码还是不能关闭错误的word进程。请问该怎么修改呢?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
5#
 楼主| 发表于 2010-1-17 23:19:01 | 只看该作者
谢谢楼上的朋友,收藏了~~~
4#
发表于 2010-1-15 19:02:33 | 只看该作者
andymark  的例子
3#
发表于 2010-1-15 19:01:55 | 只看该作者
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread
Private Const PROCESS_TERMINATE As Long = &H1

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Function KillApp(exe As String) As Boolean
    Dim i As Long
    Dim Proc As PROCESSENTRY32
    Dim Snap As Long
    Dim exeName As String
    Dim theLoop As Long
    Dim lHand As Long, lProcessID As Long
   
    Snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
    Proc.dwSize = Len(Proc)
    theLoop = ProcessFirst(Snap, Proc) '获取第一个进程,并得到其返回值
    Do While theLoop <> 0 '当返回值非零时继续获取下一个进程
      exeName = Proc.szExeFile  '进程名
      exeName = Left$(exeName, IIf(InStr(1, exeName, Chr$(0)) > 0, InStr(1, exeName, Chr$(0)) - 1, 0))
      lProcessID = Proc.th32ProcessID '得到进程ID
      Debug.Print exeName
      If LCase(exeName) <> LCase(exe) Then
        theLoop = ProcessNext(Snap, Proc)
      Else
        GoTo Kill
      End If
    Loop
    KillApp = False
    CloseHandle Snap '关闭进程“快照”句柄
    Exit Function
Kill:
    CloseHandle Snap '关闭进程“快照”句柄
    lHand = OpenProcess(PROCESS_TERMINATE, True, CLng(lProcessID)) '获取进程句柄
    TerminateProcess lHand, 0 '关闭进程
    KillApp = True
End Function

Private Sub Command2_Click()
KillApp (Me.Text0)
End Sub
2#
发表于 2010-1-15 17:02:24 | 只看该作者
似乎应该不要wordDoc.Close   ,直接n.Quit。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-17 23:44 , Processed in 0.087659 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表