HOW TO close Excel instance started by mail merge

前端 未结 2 606
渐次进展
渐次进展 2021-01-24 11:14

HOW TO close Excel instance started by mail merge

this code running inside launcher does not have access to Excel running via DDE ??

\'For i = 1 To Work         


        
相关标签:
2条回答
  • 2021-01-24 11:52

    Excel can be closed via VBA, if called from within Excel

    Application.Quit
    

    If called from outside Excel, you will need to set a reference to Excel and then close it.

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

    You need to ensure that all Workbooks are closed or saved, otherwise Excel will prompt the user to save.

    0 讨论(0)
  • 2021-01-24 11:55

    You can kill the excel process like so: (from 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;
    }
    
    0 讨论(0)
提交回复
热议问题