MS Office同步器研发心得(1)之自动化
2004-07-06 20:56 灵感之源 阅读(1969) 评论(6) 编辑 收藏 举报这是MS Office应用开发心得的第一篇,我将结合实际项目研发把心得写出来。
这个MS Office同步器是使用VB6开发的,本系列的后半章节我会把.NET的实现写出来。所以,如果大家对VB6不感兴趣,可以忽略,不过,如果你对Office应用开发比较感兴趣,仍然可以继续看下去,因为具体实现的语言是VB,但目的仍然是Office应用开发。
相关的一些信息我已经在以前的帖子中写过,大家可以参看一下,我不再复述。
从架构上来说,我实现了在VB6中利用HTTP跟ASP.NET进行交互,把本地Office信息(Outlook中的联系人、任务、日程、便笺、公告和邮件等等)与远程服务器进行同步。
或许大家会疑惑,为什么我会选择VB6而不选择.NET来开发,答案是:关于轻量级客户端的一些想法。
其特点是:Outlook应用开发(3)之插件引擎
其技术难题是:http://www.cnblogs.com/unruledboy/archive/2004/06/19/17076.aspx
下载:https://files.cnblogs.com/unruledboy/AddIn.zip
需要说明的是,这个Office同步器是一个比较抽象的东西,他通过外部对象定义属性列表来确定同步的信息,意思就是说,任何同步的对象和具体属性都可以在外部配置文件中定义,不需要改动程序。
这个Office同步器是以插件形式出现的,要设计Office插件,必须先引用几个类库:
1、Microsoft Add-In Designer:微软插件设计器
2、Microsoft Office xx.0 Object Library:Office类库
3、Microsoft Outlook xx.0 Object Library:Outlook类库
微软插件设计器的作用是简化微软支持通过COM技术来实现的插件的编写工作;Office类库的主要功能是可以实现对Office系列产品通用的菜单和工具栏等对象的访问;Outlook类库当然就是访问其拥有信息了:联系人、任务、日程、便笺、公告和邮件等等。
新增一Office设计器,VB6自动添加了相关的接口,以下是该设计器的代码:
'*
'* 名称:dsrOutlook
'*
'* 功能:Add-In主入口
'*
'* 作者:chs(wilson chan)
'*
'* 创建:2004/6/14
'*
'* 更新:2004/6/25 10:51
'*
'* 说明:
'*
'*************************************************************************
Option Explicit
' Implement extensibility library.
Implements IDTExtensibility2
Private WithEvents p_mctlBtnEvents As Office.CommandBarButton
Private WithEvents p_mctlBtnEvents2 As Office.CommandBarButton
Private Sub AddinInstance_Initialize()
' 插件启动
End Sub
Private Sub AddinInstance_Terminate()
' 插件关闭
End Sub
Private Sub p_mctlBtnEvents_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
'打开您的窗口
End Sub
Private Sub p_mctlBtnEvents2_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
'打开您的窗口
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
' Debug.Print OnBeginShutdown
End Sub
'插件加载,应用程序启动完成
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Set p_objAppInstance = Application
Set p_mctlBtnEvents = CreateAddInCommandBarButton(Application, ConnectMode, AddInInst, True)
Set p_mctlBtnEvents2 = CreateAddInCommandBarButton(Application, ConnectMode, AddInInst, False)
End Sub
'应用程序关闭
Private Sub IDTExtensibility2_OnDisconnection(ByVal _
RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
custom() As Variant)
RemoveAddInCommandBarButton RemoveMode, True
RemoveAddInCommandBarButton RemoveMode, False
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'Debug.Print "OnStartupComplete"
End Sub
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'Debug.Print "OnAddInsUpdate"
End Sub
以下是插件代码:
'*
'* 名称:basAddIn
'*
'* 功能:Add-In控制
'*
'* 作者:chs(wilson chan)
'*
'* 创建:2004/6/14
'*
'* 更新:2004/6/25 10:51
'*
'* 说明:
'*
'*************************************************************************
Option Explicit
' Constants for characters surrounding ProgID.
Public Const PROG_ID_START As String = "!<"
Public Const PROG_ID_END As String = ">"
' Constants for menu item in Office application.
Public Const MENU_TOOLS_CTL_NAME As String = "Tools"
Public Const MENU_TOOLS_CTL_KEY As String = "SmartOfficeSync"
Public Const MENU_TOOLS_CTL_CAPTION As String = "智能&Office同步器"
Public Const TOOLBAR_STANDARD_CTL_NAME As String = "Standard"
Public Const TOOLBAR_STANDARD_CTL_KEY As String = "SmartOfficeSync2"
Public Const TOOLBAR_STANDARD_CTL_CAPTION As String = "智能Office同步器"
Public p_objAppInstance As Object
Public Sub RaiseError(objError As ErrObject)
Dim strMsg As String
strMsg = "错误出现在 COM add-in,名称为 '" _
& App.Title & "'." & vbCrLf & "Error #:" & objError.Number _
& vbCrLf & objError.Description
MsgBox strMsg, , "错误"
End Sub
'添加按钮
Public Function CreateAddInCommandBarButton(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, _
ByVal blnMenu As Boolean) As Office.CommandBarButton
Dim cbrMenu As Office.CommandBar
Dim ctlMenuItem As Office.CommandBarButton
On Error GoTo HandleError
Set cbrMenu = p_objAppInstance.ActiveExplorer.CommandBars(IIf(blnMenu, p_strMENU_TOOLS_CTL_NAME, p_strTOOLBAR_STANDARD_CTL_NAME))
Set ctlMenuItem = cbrMenu.FindControl(Tag:=IIf(blnMenu, MENU_TOOLS_CTL_KEY, TOOLBAR_STANDARD_CTL_KEY))
If ctlMenuItem Is Nothing Then
Set ctlMenuItem = cbrMenu.Controls.Add(Type:=msoControlButton, _
Parameter:=IIf(blnMenu, MENU_TOOLS_CTL_KEY, TOOLBAR_STANDARD_CTL_KEY))
With ctlMenuItem
.Caption = IIf(blnMenu, p_strMENU_TOOLS_CTL_CAPTION, p_strTOOLBAR_STANDARD_CTL_CAPTION)
.Tag = IIf(blnMenu, MENU_TOOLS_CTL_KEY, TOOLBAR_STANDARD_CTL_KEY)
.Style = msoButtonCaption
.OnAction = PROG_ID_START & AddInInst.ProgId _
& PROG_ID_END
End With
End If
Set CreateAddInCommandBarButton = ctlMenuItem
CreateAddInCommandBarButton_End:
Exit Function
HandleError:
RaiseError Err
Resume CreateAddInCommandBarButton_End
End Function
'移除按钮
Public Function RemoveAddInCommandBarButton(Optional ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
Optional ByVal blnMenu As Boolean = True)
On Error GoTo HandleError
If RemoveMode = ext_dm_UserClosed Then
On Error Resume Next
p_objAppInstance.CommandBars(IIf(blnMenu, p_strMENU_TOOLS_CTL_NAME, p_strTOOLBAR_STANDARD_CTL_NAME)).Controls(IIf(blnMenu, MENU_TOOLS_CTL_KEY, TOOLBAR_STANDARD_CTL_KEY)).Delete
On Error GoTo HandleError
End If
RemoveAddInCommandBarButton_End:
Exit Function
HandleError:
RaiseError Err
Resume RemoveAddInCommandBarButton_End
End Function
(待续...)