KeithRoberts
12-15-2008, 04:32 PM
I am adding a toolbar with four buttons to an application. The add is successful, but the buttons will not launch the macros (they are functions). If I add the toolbar manually, all buttons work fine. If the toolbar is added via VBA, the toolbar is visible, but the buttons do not work. When I check them, the assign macro..., and the reset popup menu items are grayed out.
I am not sure what I am doing wrong. I am posting my code in the hopes that someone can help me figure this out.
I am reading an INI file, checking the version and if the versions are different, deleting the old bar and adding the new bar from the control INI file.
Function CreateProjectReportToolbar()
'This module creates the project toolbar. If it exists, then it updates it.
Dim cmdButton As CommandBarButton
Dim lngBars As Long
Dim lngCntr As Long
Dim lngCntr1 As Long
Dim lngUBound As Long
Dim objWD As Object
Dim strAdaptiveMenu As String
Dim strApplication As String
Dim strBarID As String
Dim strBuiltIn As String
Dim strBuiltInFace As String
Dim strCaption As String
Dim strDescriptionText As String
Dim strEnabled As String
Dim strFaceId As String
Dim strHeight As String
Dim strIndex As String
Dim strLeft As String
Dim strMask As String
Dim strName As String
Dim strName1 As String
Dim strOnAction As String
Dim strParameter As String
Dim strParameter1 As String
Dim strPathAndFileName As String
Dim strPosition As String
Dim strPriority As String
Dim strRowIndex As String
Dim strSection As String
Dim strShortcutText As String
Dim strStyle As String
Dim strTag As String
Dim strTooltipText As String
Dim strTop As String
Dim strType As String
Dim strVisible As String
Dim strWidth As String
Dim varProjButtons As Variant
Dim varProjReports As Variant
strPathAndFileName = "C:\PSSP\ToolBar\Version.ini"
strSection = "ProjectReports"
varProjReports = qsys_GetIniSectionValues(strPathAndFileName, strSection)
For lngCntr = 0 To UBound(varProjReports, 1)
Select Case varProjReports(lngCntr, 0)
Case "Name"
strName = varProjReports(lngCntr, 1)
Case "NumberofBars"
lngBars = varProjReports(lngCntr, 1)
Case "BarID"
strBarID = varProjReports(lngCntr, 1)
Case "AdaptiveMenu"
strAdaptiveMenu = varProjReports(lngCntr, 1)
Case "Application"
strApplication = varProjReports(lngCntr, 1)
Case "Builtin"
strBuiltIn = varProjReports(lngCntr, 1)
Case "Enabled"
strEnabled = varProjReports(lngCntr, 1)
Case "Height"
strHeight = varProjReports(lngCntr, 1)
Case "Index"
strIndex = varProjReports(lngCntr, 1)
Case "Left"
strLeft = varProjReports(lngCntr, 1)
Case "Position"
strPosition = varProjReports(lngCntr, 1)
Case "RowIndex"
strRowIndex = varProjReports(lngCntr, 1)
Case "Top"
strTop = varProjReports(lngCntr, 1)
Case "Type"
strType = varProjReports(lngCntr, 1)
Case "Visible"
strVisible = varProjReports(lngCntr, 1)
Case "Width"
strWidth = varProjReports(lngCntr, 1)
End Select
Next lngCntr
On Error Resume Next
MSProject.Application.CommandBars(strName).Delete
Application.CommandBars.Add(Name:=strName, Position:=msoBarFloating).Visible = strVisible
Application.CommandBars(strName).AdaptiveMenu = strAdaptiveMenu
Application.CommandBars(strName).Application = strApplication
Application.CommandBars(strName).Enabled = strEnabled
Application.CommandBars(strName).Height = strHeight
Application.CommandBars(strName).Left = strLeft
Application.CommandBars(strName).Name = strName
Application.CommandBars(strName).Position = msoBarMenuBar
Application.CommandBars(strName).RowIndex = strRowIndex
Application.CommandBars(strName).Top = strTop
MSProject.Application.CommandBars(strName).Width = strWidth
For lngCntr = 1 To lngBars
strSection = strName & "-" & strBarID & "-" & lngCntr & "-CommandButton"
varProjButtons = qsys_GetIniSectionValues(strPathAndFileName, strSection)
lngUBound = UBound(varProjButtons, 1)
For lngCntr1 = 0 To lngUBound
Select Case varProjButtons(lngCntr1, 0)
Case "BuiltIn"
strBuiltIn = varProjButtons(lngCntr1, 1)
Case "BuiltInFace"
strBuiltInFace = varProjButtons(lngCntr1, 1)
Case "Caption"
strCaption = varProjButtons(lngCntr1, 1)
Case "DescriptionText"
strDescriptionText = varProjButtons(lngCntr1, 1)
Case "Enabled"
strEnabled = varProjButtons(lngCntr1, 1)
Case "FaceId"
strFaceId = varProjButtons(lngCntr1, 1)
Case "Height"
strHeight = varProjButtons(lngCntr1, 1)
Case "Index"
strIndex = varProjButtons(lngCntr1, 1)
Case "Left"
strLeft = varProjButtons(lngCntr1, 1)
Case "Mask"
strMask = varProjButtons(lngCntr1, 1)
Case "OnAction"
strOnAction = varProjButtons(lngCntr1, 1)
Case "Parameter"
strParameter = varProjButtons(lngCntr1, 1)
Case "Priority"
strPriority = varProjButtons(lngCntr1, 1)
Case "ShortcutText"
strShortcutText = varProjButtons(lngCntr1, 1)
Case "Style"
strStyle = varProjButtons(lngCntr1, 1)
Case "Tag"
strTag = varProjButtons(lngCntr1, 1)
Case "TooltipText"
strTooltipText = varProjButtons(lngCntr1, 1)
Case "Top"
strTop = varProjButtons(lngCntr1, 1)
Case "Type"
strType = varProjButtons(lngCntr1, 1)
Case "Visible"
strVisible = varProjButtons(lngCntr1, 1)
Case "Width"
strWidth = varProjButtons(lngCntr1, 1)
End Select
Next lngCntr1
Set cmdButton = CommandBars(strName).Controls.Add
strParameter1 = ""
For lngCntr1 = 1 To Len(strParameter)
If Mid(strParameter, lngCntr1, 1) = """" Then
strParameter1 = strParameter1 & """" & """"
Else
strParameter1 = strParameter1 & Mid(strParameter, lngCntr1, 1)
End If
Next lngCntr1
strParameter = strParameter1
cmdButton.BuiltInFace = strBuiltInFace
cmdButton.Caption = strCaption
cmdButton.DescriptionText = strDescriptionText
cmdButton.Enabled = strEnabled
cmdButton.FaceId = strFaceId
cmdButton.OnAction = strOnAction
cmdButton.Parameter = strParameter
cmdButton.Priority = strPriority
cmdButton.ShortcutText = strShortcutText
cmdButton.Style = strStyle
cmdButton.Tag = strTag
cmdButton.TooltipText = strTooltipText
cmdButton.Visible = strVisible
cmdButton.Width = strWidth
Next lngCntr
I am not sure what I am doing wrong. I am posting my code in the hopes that someone can help me figure this out.
I am reading an INI file, checking the version and if the versions are different, deleting the old bar and adding the new bar from the control INI file.
Function CreateProjectReportToolbar()
'This module creates the project toolbar. If it exists, then it updates it.
Dim cmdButton As CommandBarButton
Dim lngBars As Long
Dim lngCntr As Long
Dim lngCntr1 As Long
Dim lngUBound As Long
Dim objWD As Object
Dim strAdaptiveMenu As String
Dim strApplication As String
Dim strBarID As String
Dim strBuiltIn As String
Dim strBuiltInFace As String
Dim strCaption As String
Dim strDescriptionText As String
Dim strEnabled As String
Dim strFaceId As String
Dim strHeight As String
Dim strIndex As String
Dim strLeft As String
Dim strMask As String
Dim strName As String
Dim strName1 As String
Dim strOnAction As String
Dim strParameter As String
Dim strParameter1 As String
Dim strPathAndFileName As String
Dim strPosition As String
Dim strPriority As String
Dim strRowIndex As String
Dim strSection As String
Dim strShortcutText As String
Dim strStyle As String
Dim strTag As String
Dim strTooltipText As String
Dim strTop As String
Dim strType As String
Dim strVisible As String
Dim strWidth As String
Dim varProjButtons As Variant
Dim varProjReports As Variant
strPathAndFileName = "C:\PSSP\ToolBar\Version.ini"
strSection = "ProjectReports"
varProjReports = qsys_GetIniSectionValues(strPathAndFileName, strSection)
For lngCntr = 0 To UBound(varProjReports, 1)
Select Case varProjReports(lngCntr, 0)
Case "Name"
strName = varProjReports(lngCntr, 1)
Case "NumberofBars"
lngBars = varProjReports(lngCntr, 1)
Case "BarID"
strBarID = varProjReports(lngCntr, 1)
Case "AdaptiveMenu"
strAdaptiveMenu = varProjReports(lngCntr, 1)
Case "Application"
strApplication = varProjReports(lngCntr, 1)
Case "Builtin"
strBuiltIn = varProjReports(lngCntr, 1)
Case "Enabled"
strEnabled = varProjReports(lngCntr, 1)
Case "Height"
strHeight = varProjReports(lngCntr, 1)
Case "Index"
strIndex = varProjReports(lngCntr, 1)
Case "Left"
strLeft = varProjReports(lngCntr, 1)
Case "Position"
strPosition = varProjReports(lngCntr, 1)
Case "RowIndex"
strRowIndex = varProjReports(lngCntr, 1)
Case "Top"
strTop = varProjReports(lngCntr, 1)
Case "Type"
strType = varProjReports(lngCntr, 1)
Case "Visible"
strVisible = varProjReports(lngCntr, 1)
Case "Width"
strWidth = varProjReports(lngCntr, 1)
End Select
Next lngCntr
On Error Resume Next
MSProject.Application.CommandBars(strName).Delete
Application.CommandBars.Add(Name:=strName, Position:=msoBarFloating).Visible = strVisible
Application.CommandBars(strName).AdaptiveMenu = strAdaptiveMenu
Application.CommandBars(strName).Application = strApplication
Application.CommandBars(strName).Enabled = strEnabled
Application.CommandBars(strName).Height = strHeight
Application.CommandBars(strName).Left = strLeft
Application.CommandBars(strName).Name = strName
Application.CommandBars(strName).Position = msoBarMenuBar
Application.CommandBars(strName).RowIndex = strRowIndex
Application.CommandBars(strName).Top = strTop
MSProject.Application.CommandBars(strName).Width = strWidth
For lngCntr = 1 To lngBars
strSection = strName & "-" & strBarID & "-" & lngCntr & "-CommandButton"
varProjButtons = qsys_GetIniSectionValues(strPathAndFileName, strSection)
lngUBound = UBound(varProjButtons, 1)
For lngCntr1 = 0 To lngUBound
Select Case varProjButtons(lngCntr1, 0)
Case "BuiltIn"
strBuiltIn = varProjButtons(lngCntr1, 1)
Case "BuiltInFace"
strBuiltInFace = varProjButtons(lngCntr1, 1)
Case "Caption"
strCaption = varProjButtons(lngCntr1, 1)
Case "DescriptionText"
strDescriptionText = varProjButtons(lngCntr1, 1)
Case "Enabled"
strEnabled = varProjButtons(lngCntr1, 1)
Case "FaceId"
strFaceId = varProjButtons(lngCntr1, 1)
Case "Height"
strHeight = varProjButtons(lngCntr1, 1)
Case "Index"
strIndex = varProjButtons(lngCntr1, 1)
Case "Left"
strLeft = varProjButtons(lngCntr1, 1)
Case "Mask"
strMask = varProjButtons(lngCntr1, 1)
Case "OnAction"
strOnAction = varProjButtons(lngCntr1, 1)
Case "Parameter"
strParameter = varProjButtons(lngCntr1, 1)
Case "Priority"
strPriority = varProjButtons(lngCntr1, 1)
Case "ShortcutText"
strShortcutText = varProjButtons(lngCntr1, 1)
Case "Style"
strStyle = varProjButtons(lngCntr1, 1)
Case "Tag"
strTag = varProjButtons(lngCntr1, 1)
Case "TooltipText"
strTooltipText = varProjButtons(lngCntr1, 1)
Case "Top"
strTop = varProjButtons(lngCntr1, 1)
Case "Type"
strType = varProjButtons(lngCntr1, 1)
Case "Visible"
strVisible = varProjButtons(lngCntr1, 1)
Case "Width"
strWidth = varProjButtons(lngCntr1, 1)
End Select
Next lngCntr1
Set cmdButton = CommandBars(strName).Controls.Add
strParameter1 = ""
For lngCntr1 = 1 To Len(strParameter)
If Mid(strParameter, lngCntr1, 1) = """" Then
strParameter1 = strParameter1 & """" & """"
Else
strParameter1 = strParameter1 & Mid(strParameter, lngCntr1, 1)
End If
Next lngCntr1
strParameter = strParameter1
cmdButton.BuiltInFace = strBuiltInFace
cmdButton.Caption = strCaption
cmdButton.DescriptionText = strDescriptionText
cmdButton.Enabled = strEnabled
cmdButton.FaceId = strFaceId
cmdButton.OnAction = strOnAction
cmdButton.Parameter = strParameter
cmdButton.Priority = strPriority
cmdButton.ShortcutText = strShortcutText
cmdButton.Style = strStyle
cmdButton.Tag = strTag
cmdButton.TooltipText = strTooltipText
cmdButton.Visible = strVisible
cmdButton.Width = strWidth
Next lngCntr