Dim xlApp As Excel.Application '定义EXCEL类 Dim xlBook As Excel.Workbook '定义工件簿类 Dim xlsheet As Excel.Worksheet '定义工作表类………………xlBook.Close savechanges:=True xlApp.Quit (如果想关掉不保存) xlBook.Close savechanges:=false xlApp.Quit
可以啊 就像你说的那样,Quit一下就行! 当你把vb程序关了之后,进程里就没有了
xlApp.Quit set xlApp=nothing
完整示例: Dim xlApp As Object 'Excel.Application Dim xlBook As Object 'Excel.Workbook Dim xlSheet As Object 'Excel.Worksheet Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Add Set xlSheet = xlBook.worksheets(1) With xlSheet .Range("A6:A7").Select End With xlApp.ActiveCell.FormulaR1C1 = "编号" xlBook.Saved = False xlBook.SaveAs "C:\1.xls" 'For Each xlSheet In xlBook.worksheets 'Set xlSheet = Nothing 'Next Set xlSheet = Nothing xlBook.Close Set xlBook = Nothing xlApp.Quit Set xlApp = Nothing
上两天刚弄完的,希望对你有帮助! Public Const PROCESS_TERMINATE = &H1Public Const MAX_PATH = 260Public 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 * MAX_PATH End TypePublic Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As Long Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As LongPublic Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long 'Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public Const TH32CS_SNAPHEAPLIST = &H1 Public Const TH32CS_SNAPPROCESS = &H2 Public Const TH32CS_SNAPTHREAD = &H4 Public Const TH32CS_SNAPMODULE = &H8 Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE) Public Const TH32CS_INHERIT = &H80000000 Public Function KillPowerPointProcessID() As Boolean Dim strProcessName As String Dim lngProcessID As Long Dim hand As Long On Error Resume Next Dim hSnapshot As Long, lRet As Long, P As PROCESSENTRY32 P.dwSize = Len(P) hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, ByVal 0) If hSnapshot Then lRet = Process32First(hSnapshot, P) Do While lRet strProcessName = Left$(P.szExeFile, InStr(P.szExeFile, Chr$(0)) - 1) If LCase(strProcessName) = LCase("Excel.exe") Then lngProcessID = P.th32ProcessID hand = OpenProcess(PROCESS_TERMINATE, True, lngProcessID) '获取进程句柄 TerminateProcess hand, 0 '关闭进程 End If
lRet = Process32Next(hSnapshot, P) Loop lRet = CloseHandle(hSnapshot) End If If Err.Number <> 0 Then KillPowerPointProcessID = False Else KillPowerPointProcessID = True End If Err.Clear End Function
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类………………xlBook.Close savechanges:=True
xlApp.Quit
(如果想关掉不保存)
xlBook.Close savechanges:=false
xlApp.Quit
就像你说的那样,Quit一下就行!
当你把vb程序关了之后,进程里就没有了
set xlApp=nothing
Dim xlApp As Object 'Excel.Application
Dim xlBook As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.worksheets(1)
With xlSheet
.Range("A6:A7").Select
End With
xlApp.ActiveCell.FormulaR1C1 = "编号"
xlBook.Saved = False
xlBook.SaveAs "C:\1.xls"
'For Each xlSheet In xlBook.worksheets
'Set xlSheet = Nothing
'Next
Set xlSheet = Nothing
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
需要清空剪切板,然后xlapp.quit才能退出时不留下进程...
不相信你们试试
Public Const PROCESS_TERMINATE = &H1Public Const MAX_PATH = 260Public 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 * MAX_PATH
End TypePublic Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As LongPublic Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Const TH32CS_SNAPHEAPLIST = &H1
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPTHREAD = &H4
Public Const TH32CS_SNAPMODULE = &H8
Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE)
Public Const TH32CS_INHERIT = &H80000000
Public Function KillPowerPointProcessID() As Boolean
Dim strProcessName As String
Dim lngProcessID As Long
Dim hand As Long
On Error Resume Next Dim hSnapshot As Long, lRet As Long, P As PROCESSENTRY32
P.dwSize = Len(P)
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, ByVal 0)
If hSnapshot Then
lRet = Process32First(hSnapshot, P)
Do While lRet
strProcessName = Left$(P.szExeFile, InStr(P.szExeFile, Chr$(0)) - 1)
If LCase(strProcessName) = LCase("Excel.exe") Then
lngProcessID = P.th32ProcessID
hand = OpenProcess(PROCESS_TERMINATE, True, lngProcessID) '获取进程句柄
TerminateProcess hand, 0 '关闭进程
End If
lRet = Process32Next(hSnapshot, P)
Loop
lRet = CloseHandle(hSnapshot)
End If
If Err.Number <> 0 Then
KillPowerPointProcessID = False
Else
KillPowerPointProcessID = True
End If
Err.Clear
End Function