水如烟

秋里生活,恬淡宁静。那如洗碧空,那伏黄草地,风凉夜寂,心儿涌动,情绪萦绕。可以凭窗,可以临江,可以坐,可以仰卧,可以独处,可以相依。倚明月,抚清辉,人生多少情怀,尽在秋里?

原创 HOW TO:获取ExcelApplication COM对象的进程收藏

新一篇: HOW TO:监视Window空闲时间 | 旧一篇: HOW TO:Office文档版本的检测

Author:水如烟 

代码

Option Strict Off

Imports System.Runtime.InteropServices

Public Class ExcelDemo
    
Private gApplication As Object
    
Private gProcess As Process

    
Public ReadOnly Property Application() As Object
        
Get
            
Return gApplication
        
End Get
    
End Property

    
Public ReadOnly Property Process() As Process
        
Get
            
Return gProcess
        
End Get
    
End Property

    
Sub New()
        gApplication 
= CreateObject("Excel.Application")

        
Dim hwnd As Integer = CInt(gApplication.Hwnd)

        
Dim processid As Integer
        GetWindowThreadProcessId(hwnd, processid)

        gProcess 
= Process.GetProcessById(processid)

        gApplication.Visible 
= True
    
End Sub

    
Public Sub Kill()
        
Me.Process.Kill()
    
End Sub

    
<DllImport("user32.dll", SetLastError:=True)> _
    
Private Shared Function GetWindowThreadProcessId( _
    
ByVal handle As Integer, _
    
<Out()> ByRef processId As IntegerAs Integer
    
End Function
End Class

 

 示例:

Public Class Form1

    
Private excel As ExcelDemo
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        excel 
= New ExcelDemo
    
End Sub

    
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        excel.Kill()
    
End Sub
End Class

 

 

 

发表于 @ 2008年04月26日 21:32:11|评论(loading...)|编辑

新一篇: HOW TO:监视Window空闲时间 | 旧一篇: HOW TO:Office文档版本的检测

评论:没有评论。

发表评论  


登录
Csdn Blog version 3.1a
Copyright © 水如烟