水如烟

                 顺其自然,水到渠成 LzmTW

文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.

导航

MSOffice小知识:基本框架

Posted on 2006-05-26 18:38  水如烟(LzmTW)  阅读(517)  评论(0编辑  收藏  举报

Author:水如烟

在前面说过,OFFICE(这里指Excel、Word、Access)类似处甚多,为组织方面,做一个基本框架还是必要的。
因为我主要的是为了以后“讲述”各自的特有对象方便,而不是真正的用在专业程序集方面,就当做是练习所用吧,仅作参考。
如下图:

 其中SR是读取资源用的,在我的BLOG上有相关代码,在此不列出来了。

ComobjReleaseMethod.VB(com对象的释放)

Namespace uOffice
    
Friend Class ComObjReleaseMethod

        
Friend Shared Sub Invoke(ByVal comObj As ObjectByVal methodName As StringByVal parameters() As Object)
            
Dim mMethod As Reflection.MethodInfo = comObj.GetType.GetMethod(methodName)
            mMethod.Invoke(comObj, parameters)
        
End Sub

        
Friend Shared Sub ReleaseComObject(ByVal comObj As Object)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(comObj)
            comObj 
= Nothing
        
End Sub

        
Friend Shared Sub KillProcess(ByVal comObjProcessName As StringByVal beforeProcessStartTime As DateByVal afterProcessStartTime As Date)

            
Dim mProcessList As Process()
            
Dim mProcessStartTime As Date

            mProcessList 
= Process.GetProcessesByName(comObjProcessName)

            
For Each tmpProcess As Process In mProcessList
                mProcessStartTime 
= tmpProcess.StartTime
                
If mProcessStartTime.CompareTo(beforeProcessStartTime) > 0 AndAlso mProcessStartTime.CompareTo(afterProcessStartTime) < 0 Then
                    tmpProcess.Kill()
                
End If
            
Next

        
End Sub

        
Friend Shared Sub KillProcess(ByVal comObjProcessName As String)

            
Dim mProcessList As Process()

            mProcessList 
= Process.GetProcessesByName(comObjProcessName)

            
For Each tmpProcess As Process In mProcessList
                tmpProcess.Kill()
            
Next

        
End Sub

    
End Class
End Namespace


ApplicationEnum.VB (Office三个应用对象)

Namespace uOffice
    
Friend Enum ApplicationEnum
        Access
        Excel
        Word
    
End Enum
End Namespace


ApplicationBase.VB(Office三个对象的基类)

Option Strict Off
Namespace uOffice
    
Public MustInherit Class ApplicationBase
        
Implements IDisposable

        
Friend gOfficeApplication As ApplicationEnum
        
Protected gApplicationObject As Object

        
Private gBeforeProcessStartTime As Date
        
Private gAfterProcessStartTime As Date

        
Protected Sub CreateInstance()
            gBeforeProcessStartTime 
= Now
            
Select Case gOfficeApplication
                
Case ApplicationEnum.Access
                    gApplicationObject 
= CreateObject(SR.GetString("Office_Application_Access"))
                
Case ApplicationEnum.Excel
                    gApplicationObject 
= CreateObject(SR.GetString("Office_Application_Excel"))
                
Case ApplicationEnum.Word
                    gApplicationObject 
= CreateObject(SR.GetString("Office_Application_Word"))
            
End Select
            gAfterProcessStartTime 
= Now
        
End Sub

        
''' <summary>
        ''' 退出主进程
        ''' </summary>
        Public Sub Quit()
            
'置回默认设置,如Excel.DisplayAlerts = True
            ResetDefaultPropertiesBeforeApplicationRelease()
            
'释放其它对象,如Excel.Worksheets
            RealseInternalComObjectsBeforeApplicationRelease()
            
'释放主进程,如Excel
            Application_Quit()
            
'保证完全退出
            ApplicationRelease()
        
End Sub

        
''' <summary>
        ''' 退出其它Com对象
        ''' </summary>
        Protected MustOverride Sub RealseInternalComObjectsBeforeApplicationRelease()

        
Protected Overridable Sub Application_Quit()
            gApplicationObject.Quit()
        
End Sub

        
''' <summary>
        ''' 退出OfficeApplication进程
        ''' </summary>
        Private Sub ApplicationRelease()
            ComObjReleaseMethod.ReleaseComObject(gApplicationObject)
            
Select Case gOfficeApplication
                
Case ApplicationEnum.Access
                    ComObjReleaseMethod.KillProcess(SR.GetString(
"Office_ProcessName_Access"), gBeforeProcessStartTime, gAfterProcessStartTime)
                
Case ApplicationEnum.Excel
                    ComObjReleaseMethod.KillProcess(SR.GetString(
"Office_ProcessName_Excel"), gBeforeProcessStartTime, gAfterProcessStartTime)
                
Case ApplicationEnum.Word
                    ComObjReleaseMethod.KillProcess(SR.GetString(
"Office_ProcessName_Word"), gBeforeProcessStartTime, gAfterProcessStartTime)
            
End Select
        
End Sub

        
''' <summary>
        ''' 保存默认设置
        ''' </summary>
        Protected MustOverride Sub SaveDefaultPropertiesWhenApplicationInitialize()

        
''' <summary>
        ''' 置回默认设置
        ''' </summary>
        Protected MustOverride Sub ResetDefaultPropertiesBeforeApplicationRelease()

        
'///以下为实现IDisposable接口IDE自动创建的代码
        Private disposedValue As Boolean = False        ' To detect redundant calls

        
' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            
If Not Me.disposedValue Then
                
If disposing Then
                    
' TODO: free unmanaged resources when explicitly called
                    Quit()
                
End If

                
' TODO: free shared unmanaged resources
            End If
            
Me.disposedValue = True
        
End Sub

#Region " IDisposable Support "
        
' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            
' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(
Me)
        
End Sub
#End Region

    
End Class
End Namespace


ApplicationBaseCommon.VB(同上,Partial,只是分列出来,列举相同的函数或属性)

Option Strict Off
'//通用方法及属性
Namespace uOffice
    Partial 
Public Class ApplicationBase

        
Public Sub SetVisible(ByVal visible As Boolean)
            
Me.gApplicationObject.Visible = visible
        
End Sub

        
Public ReadOnly Property Version() As String
            
Get
                
Return Me.gApplicationObject.Version
            
End Get
        
End Property

    
End Class
End Namespace


实现了基类的,有待充实的三个对象:
AccessApplication.VB

Namespace uOffice
    
Public Class AccessApplication
        
Inherits ApplicationBase

        
Protected Overrides Sub SaveDefaultPropertiesWhenApplicationInitialize()

        
End Sub

        
Protected Overrides Sub ResetDefaultPropertiesBeforeApplicationRelease()

        
End Sub

        
Protected Overrides Sub RealseInternalComObjectsBeforeApplicationRelease()

        
End Sub

        
Sub New()
            
Me.gOfficeApplication = ApplicationEnum.Access
            
Me.CreateInstance()
        
End Sub

        
'编码时参考,最后将置为 As Object
        Public ReadOnly Property CurrentApplication() As Microsoft.Office.Interop.Access.Application 'As Object
            Get
                
Return DirectCast(Me.gApplicationObject, Microsoft.Office.Interop.Access.Application) 'Return Me.gApplicationObject
            End Get
        
End Property

    
End Class
End Namespace


ExcelApplication.VB

Namespace uOffice
    
Public Class ExcelApplication
        
Inherits ApplicationBase

        
Protected Overrides Sub SaveDefaultPropertiesWhenApplicationInitialize()

        
End Sub

        
Protected Overrides Sub ResetDefaultPropertiesBeforeApplicationRelease()

        
End Sub

        
Protected Overrides Sub RealseInternalComObjectsBeforeApplicationRelease()

        
End Sub

        
Sub New()
            
Me.gOfficeApplication = ApplicationEnum.Excel
            
Me.CreateInstance()
        
End Sub

        
'编码时参考,最后将置为 As Object
        Public ReadOnly Property CurrentApplication() As Microsoft.Office.Interop.Excel.Application 'As Object
            Get
                
Return DirectCast(Me.gApplicationObject, Microsoft.Office.Interop.Excel.Application) 'Return Me.gApplicationObject
            End Get
        
End Property

    
End Class
End Namespace


WordApplication.VB

Namespace uOffice
    
Public Class WordApplication
        
Inherits ApplicationBase

        
Protected Overrides Sub SaveDefaultPropertiesWhenApplicationInitialize()

        
End Sub

        
Protected Overrides Sub ResetDefaultPropertiesBeforeApplicationRelease()

        
End Sub

        
Protected Overrides Sub RealseInternalComObjectsBeforeApplicationRelease()

        
End Sub

        
Sub New()
            
Me.gOfficeApplication = ApplicationEnum.Word
            
Me.CreateInstance()
        
End Sub

        
'编码时参考,最后将置为 As Object
        Public ReadOnly Property CurrentApplication() As Microsoft.Office.Interop.Word.Application 'As Object
            Get
                
Return DirectCast(Me.gApplicationObject, Microsoft.Office.Interop.Word.Application) 'Return Me.gApplicationObject
            End Get
        
End Property
    
End Class
End Namespace


在后面的“文章”中,将是对上面三个对象的“关注”功能进行实现。只实现我认为关注的,全部列出来是不现实的。
现在可以测试看看:

    Dim office As LzmTW.uOffice.ApplicationBase
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        office.SetVisible(
True)
        Console.WriteLine(office.Version)
    
End Sub

    
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        office.SetVisible(
False)
    
End Sub

    
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        office.Dispose()
    
End Sub

    
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        office 
= New LzmTW.uOffice.WordApplication
    
End Sub



初始工程项目下载