如何关闭通过邮件合并启动的Excel实例

时间:2009-09-17 14:28:38

标签: excel vba ole

如何关闭通过邮件合并启动的Excel实例

在启动器内运行的代码无法访问通过DDE运行的Excel?

'For i = 1 To Workbooks.Count
'   MsgBox ("here" + Workbooks(i).Name)
'If (Workbooks(i).Name <> ActiveWorkbook.Name) Then
'Workbooks(i).Close
'End If
'Next i

2 个答案:

答案 0 :(得分:0)

你可以像这样杀死excel进程:(来自http://www.dreamincode.net/code/snippet1543.htm

//Namespaces needed
using System.Diagnostics;

public bool FindAndKillProcess(string name)
{
    //here we're going to get a list of all running processes on
    //the computer
    foreach (Process clsProcess in Process.GetProcesses()) {
        //now we're going to see if any of the running processes
        //match the currently running processes by using the StartsWith Method,
        //this prevents us from incluing the .EXE for the process we're looking for.
        //. Be sure to not
        //add the .exe to the name you provide, i.e: NOTEPAD,
        //not NOTEPAD.EXE or false is always returned even if
        //notepad is running
        if (clsProcess.ProcessName.StartsWith(name))
        {
            //since we found the proccess we now need to use the
            //Kill Method to kill the process. Remember, if you have
            //the process running more than once, say IE open 4
            //times the loop thr way it is now will close all 4,
            //if you want it to just close the first one it finds
            //then add a return; after the Kill
            clsProcess.Kill();
            //process killed, return true
            return true;
        }
    }
    //process not found, return false
    return false;
}

答案 1 :(得分:0)

如果从Excel中调用,可以通过VBA关闭Excel

Application.Quit

如果从Excel外部调用,则需要设置对Excel的引用,然后将其关闭。

Set appExcel = GetObject(, "Excel.Application")
appExcel.Quit

您需要确保关闭或保存所有工作簿,否则Excel将提示用户保存。