PDA

View Full Version : [SOLVED] Toolbar added via VBA does not work, assign macro is grayed out



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

Bob Phillips
12-15-2008, 05:25 PM
What about the rest of the code and the INI file?

KeithRoberts
12-16-2008, 10:46 AM
When I used the commandbar.controls.add method, I did not use the parameter field. Evidently, this must be assigned when you add commnadbarbutton object first with the .add method. I used this code and now everything works correctly:



'Set cmdButton = CommandBars(strName).Controls.Add
CommandBars(strName).Controls.Add Type:=msoControlButton, _
ID:=40001, _
Before:=lngCntr, _
Parameter:=strParameter <=====

Set cmdButton = CommandBars(strName).Controls(lngCntr)



As for the INI file, the code that I am using to read the INI file sections works correctly and returns the proper values.

Thanks for the look and assistance!!! :friends:

Bob Phillips
12-16-2008, 10:53 AM
I often add controls, and I never use the Parameter property.

KeithRoberts
12-16-2008, 03:50 PM
I do not dispute that, but this is the only way I could get it to work. It is interesting that the original code works for you, but not for me. Oh well. Now it is on the ini file manipulations.

Thanks for your help tho!!!:beerchug: