D
DB_Calgary
I've written a shared add-in for MS Project using vb.net. I'm able to build a
command bar, add two buttons to it and wire up the click events no problem.
The problem arises when I click the button that accesses some custom .net
classes and displays window's forms.
There is no problem if I only click the first button which simply iterates
over the Project's tasks, builds a string and displays it in a message box.
The other button works properly the first time it is clicked after which
neither button works. It's as though the click event handlers have been lost.
Any thoughts would be appreciated.
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private applicationObject As
Microsoft.Office.Interop.MSProject.Application
Private addInInstance As Object
Private imgConverter As New ConvertImage
Private toolBar As Microsoft.Office.Core.CommandBar
Private test1 As Microsoft.Office.Core.CommandBarButton
Private test2 As Microsoft.Office.Core.CommandBarButton
Private cn As TTiDataBridgeClient = Nothing
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
Try
applicationObject.CommandBars.Item("Test").Delete()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
Try
applicationObject = CType(application,
Microsoft.Office.Interop.MSProject.Application)
addInInstance = addInInst
If Not applicationObject Is Nothing Then
toolBar = AddToolbar(applicationObject, "Test")
End If
If Not MakeANewButton(test1, toolBar, "Test", "TestButton1", 65,
AddressOf handleClick, imgConverter.getImage(My.Resources.gear)) Then
Throw New COMException("Problem adding butting Test")
End If
If Not MakeANewButton(test2, toolBar, "Test2", "TestButton2",
1044, AddressOf handleClick2) Then
Throw New COMException("Problem adding butting Test2")
End If
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub handleClick(ByVal ctl As
Microsoft.Office.Core.CommandBarButton, ByRef cancelDefault As Boolean)
Try
displayProjectTasks()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub handleClick2(ByVal ctl As
Microsoft.Office.Core.CommandBarButton, ByRef cancelDefault As Boolean)
Try
getDataTest()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Private Function AddToolbar(ByVal projApp As
Microsoft.Office.Interop.MSProject.Application, _
ByVal toolbarName As String) As Microsoft.Office.Core.CommandBar
Dim toolBar As Microsoft.Office.Core.CommandBar = Nothing
Try
' Create a command bar for the add-in
toolBar = CType(projApp.CommandBars.Add(toolbarName, _
Microsoft.Office.Core.MsoBarPosition.msoBarTop, , True), _
Microsoft.Office.Core.CommandBar)
toolBar.Visible = True
Return toolBar
Catch
' Add exception handling here.
Return Nothing
End Try
End Function
Private Function MakeANewButton(ByVal newButton As
Microsoft.Office.Core.CommandBarButton, ByVal commandBar As _
Microsoft.Office.Core.CommandBar, ByVal caption As String, ByVal tag As
String, _
ByVal faceID As Integer, ByVal clickHandler As _
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler, _
Optional ByVal pict As stdole.IPictureDisp = Nothing) As Boolean
Try
newButton = CType(commandBar.Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton), _
Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
newButton.Tag = tag
If commandBar.Controls.Count >= 1 Then
newButton.BeginGroup = True
End If
If Not pict Is Nothing Then
newButton.Picture = pict
'newButton.Mask = imgConverter.getImage(My.Resources.Mask)
Else
newButton.FaceId = faceID
End If
AddHandler newButton.Click, clickHandler
Return True
Catch ex As System.Exception
' Add code here to handle the exception.
Return False
End Try
End Function
Private Sub getDataTest()
Try
Dim dbtype As TTi.Data.DB_TYPE = DB_TYPE.DB_ACCESS
Dim Password As String = "ThePassword"
Dim initVector As String = "thevector"
Dim url As String = "theUrl"
If Not cn Is Nothing Then
Dim login As New TTi.Forms.TTiLogin()
login.BridgePassword = Password
login.BridgeVector = initVector
login.TTiUrl = url
If login.Login Then
cn = New TTiDataBridgeClient(Password, initVector,
login.UserId, login.Password, url)
End If
login = Nothing
End If
Dim ds As DataSet = cn.LoadDataset("SELECT * FROM tblEmployee")
Dim frm As New frmTest(ds)
frm.Show()
frm = Nothing
ds = Nothing
Catch e As COMException
MsgBox("Error: " & e.ErrorCode & Environment.NewLine &
e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Private Sub displayProjectTasks()
Dim str As String = ""
Try
For i As Integer = 1 To applicationObject.Projects.Count
Dim proj As Microsoft.Office.Interop.MSProject.Project =
applicationObject.Projects.Item(i)
str += proj.Name & Environment.NewLine
For j As Integer = 1 To proj.Tasks.Count
Dim tsk As Microsoft.Office.Interop.MSProject.Task =
proj.Tasks.Item(j)
If Not tsk Is Nothing Then
str += ControlChars.Tab & tsk.Name &
Environment.NewLine
Else
str += Environment.NewLine
End If
Next
Next
MsgBox(str, MsgBoxStyle.OkOnly)
Catch e As COMException
MsgBox("Error: " & e.ErrorCode & Environment.NewLine &
e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
End Class
command bar, add two buttons to it and wire up the click events no problem.
The problem arises when I click the button that accesses some custom .net
classes and displays window's forms.
There is no problem if I only click the first button which simply iterates
over the Project's tasks, builds a string and displays it in a message box.
The other button works properly the first time it is clicked after which
neither button works. It's as though the click event handlers have been lost.
Any thoughts would be appreciated.
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private applicationObject As
Microsoft.Office.Interop.MSProject.Application
Private addInInstance As Object
Private imgConverter As New ConvertImage
Private toolBar As Microsoft.Office.Core.CommandBar
Private test1 As Microsoft.Office.Core.CommandBarButton
Private test2 As Microsoft.Office.Core.CommandBarButton
Private cn As TTiDataBridgeClient = Nothing
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
Try
applicationObject.CommandBars.Item("Test").Delete()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
Try
applicationObject = CType(application,
Microsoft.Office.Interop.MSProject.Application)
addInInstance = addInInst
If Not applicationObject Is Nothing Then
toolBar = AddToolbar(applicationObject, "Test")
End If
If Not MakeANewButton(test1, toolBar, "Test", "TestButton1", 65,
AddressOf handleClick, imgConverter.getImage(My.Resources.gear)) Then
Throw New COMException("Problem adding butting Test")
End If
If Not MakeANewButton(test2, toolBar, "Test2", "TestButton2",
1044, AddressOf handleClick2) Then
Throw New COMException("Problem adding butting Test2")
End If
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub handleClick(ByVal ctl As
Microsoft.Office.Core.CommandBarButton, ByRef cancelDefault As Boolean)
Try
displayProjectTasks()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Public Sub handleClick2(ByVal ctl As
Microsoft.Office.Core.CommandBarButton, ByRef cancelDefault As Boolean)
Try
getDataTest()
Catch e As COMException
MsgBox("Error: " & e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Private Function AddToolbar(ByVal projApp As
Microsoft.Office.Interop.MSProject.Application, _
ByVal toolbarName As String) As Microsoft.Office.Core.CommandBar
Dim toolBar As Microsoft.Office.Core.CommandBar = Nothing
Try
' Create a command bar for the add-in
toolBar = CType(projApp.CommandBars.Add(toolbarName, _
Microsoft.Office.Core.MsoBarPosition.msoBarTop, , True), _
Microsoft.Office.Core.CommandBar)
toolBar.Visible = True
Return toolBar
Catch
' Add exception handling here.
Return Nothing
End Try
End Function
Private Function MakeANewButton(ByVal newButton As
Microsoft.Office.Core.CommandBarButton, ByVal commandBar As _
Microsoft.Office.Core.CommandBar, ByVal caption As String, ByVal tag As
String, _
ByVal faceID As Integer, ByVal clickHandler As _
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler, _
Optional ByVal pict As stdole.IPictureDisp = Nothing) As Boolean
Try
newButton = CType(commandBar.Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton), _
Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
newButton.Tag = tag
If commandBar.Controls.Count >= 1 Then
newButton.BeginGroup = True
End If
If Not pict Is Nothing Then
newButton.Picture = pict
'newButton.Mask = imgConverter.getImage(My.Resources.Mask)
Else
newButton.FaceId = faceID
End If
AddHandler newButton.Click, clickHandler
Return True
Catch ex As System.Exception
' Add code here to handle the exception.
Return False
End Try
End Function
Private Sub getDataTest()
Try
Dim dbtype As TTi.Data.DB_TYPE = DB_TYPE.DB_ACCESS
Dim Password As String = "ThePassword"
Dim initVector As String = "thevector"
Dim url As String = "theUrl"
If Not cn Is Nothing Then
Dim login As New TTi.Forms.TTiLogin()
login.BridgePassword = Password
login.BridgeVector = initVector
login.TTiUrl = url
If login.Login Then
cn = New TTiDataBridgeClient(Password, initVector,
login.UserId, login.Password, url)
End If
login = Nothing
End If
Dim ds As DataSet = cn.LoadDataset("SELECT * FROM tblEmployee")
Dim frm As New frmTest(ds)
frm.Show()
frm = Nothing
ds = Nothing
Catch e As COMException
MsgBox("Error: " & e.ErrorCode & Environment.NewLine &
e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
Private Sub displayProjectTasks()
Dim str As String = ""
Try
For i As Integer = 1 To applicationObject.Projects.Count
Dim proj As Microsoft.Office.Interop.MSProject.Project =
applicationObject.Projects.Item(i)
str += proj.Name & Environment.NewLine
For j As Integer = 1 To proj.Tasks.Count
Dim tsk As Microsoft.Office.Interop.MSProject.Task =
proj.Tasks.Item(j)
If Not tsk Is Nothing Then
str += ControlChars.Tab & tsk.Name &
Environment.NewLine
Else
str += Environment.NewLine
End If
Next
Next
MsgBox(str, MsgBoxStyle.OkOnly)
Catch e As COMException
MsgBox("Error: " & e.ErrorCode & Environment.NewLine &
e.ToString, MsgBoxStyle.OkOnly)
End Try
End Sub
End Class