2009-09-17 94 views
1

啓動Excel實例如何關閉通過郵件合併如何關閉通過郵件合併

在代碼裏啓動運行沒有獲得通過DDE運行Excel啓動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 

回答

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; 
} 
0

Excel可以通過VBA在Excel中

Application.Quit 

被關閉,如果調用從如果從外面叫Excel中,您將需要設置對Excel的引用,然後關閉它。

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

您需要確保關閉或保存所有工作簿,否則Excel將提示用戶保存。