Combobox in Menu Bar Event code

K

kaak

L.S.

I Wrote this code:

Sub AddMenuItem()

Dim oMainMenuBar As Object
Dim objCommandBarComboBox As Office.CommandBarComboBox

Set oMainMenuBar = CommandBars.Item("Menu Bar")
Set objCommandBarComboBox =
oMainMenuBar.Controls.Add(msoControlEdit)

End Sub

Now I need some code that runs when the user changes the text in the
combobox and the length = 6 characters.

Can someone help me
 
J

Jean-Guy Marcil

kaak was telling us:
kaak nous racontait que :
L.S.

I Wrote this code:

Sub AddMenuItem()

Dim oMainMenuBar As Object
Dim objCommandBarComboBox As Office.CommandBarComboBox

Set oMainMenuBar = CommandBars.Item("Menu Bar")
Set objCommandBarComboBox =
oMainMenuBar.Controls.Add(msoControlEdit)

End Sub

Now I need some code that runs when the user changes the text in the
combobox and the length = 6 characters.

What kind of code?
Do you mean that you want to monitor the typing in the combobox, or you want
to wait until the user hits enter after typing?
I do not think the first one is possible, but the second certainly.

Is this some kind of homework?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

kaak

The best thing is an event handler that checks if the text in the
combobox is numeric and of lenghth 6.
But code that runs after hitting the enter key will do fine.

with regards

Jeroen
 
J

Jean-Guy Marcil

kaak was telling us:
kaak nous racontait que :
The best thing is an event handler that checks if the text in the
combobox is numeric and of lenghth 6.
But code that runs after hitting the enter key will do fine.


What is the purpose of the application? I must admit I am curious...

I do not think you can have an event handler on a menu, or at least, not
easily.

Meanwhile, play around with this code:

Option Explicit

Private Const strToolBarName = "Menu Bar"
Private Const strCtrlCaption = "Enter number"

Sub AddMenuItem()

Dim oMainMenuBar As Office.CommandBar
Dim objCommandBarComboBox As Office.CommandBarComboBox

'Do not forget to set this according to your need
CustomizationContext = ActiveDocument

Set oMainMenuBar = Application.CommandBars(strToolBarName)
Set objCommandBarComboBox = oMainMenuBar.Controls.Add(msoControlEdit)

With objCommandBarComboBox
.Caption = strCtrlCaption
.OnAction = "CheckInput"
End With

End Sub

Private Sub CheckInput()

Dim myCtrl As Office.CommandBarComboBox
Dim i As Long

Set myCtrl = Application.CommandBars(strToolBarName) _
.Controls(strCtrlCaption)

If Len(myCtrl.Text) = 6 Then
For i = 1 To 6
If Not Mid(myCtrl.Text, i, 1) Like "[0-9]" Then
MsgBox "You must type only numbers."
Exit Sub
End If
Next
MsgBox "You HAVE typed 6 numbers."
Else
MsgBox "You have NOT typed 6 numbers."
End If

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

kaak

Thnx

This will do.
The Purpose of this application is:

I want to give the users a way to import a text file depending on a
accountnumber.

Jeroen


Jean-Guy Marcil said:
kaak was telling us:
kaak nous racontait que :
The best thing is an event handler that checks if the text in the
combobox is numeric and of lenghth 6.
But code that runs after hitting the enter key will do fine.


What is the purpose of the application? I must admit I am curious...

I do not think you can have an event handler on a menu, or at least, not
easily.

Meanwhile, play around with this code:

Option Explicit

Private Const strToolBarName = "Menu Bar"
Private Const strCtrlCaption = "Enter number"

Sub AddMenuItem()

Dim oMainMenuBar As Office.CommandBar
Dim objCommandBarComboBox As Office.CommandBarComboBox

'Do not forget to set this according to your need
CustomizationContext = ActiveDocument

Set oMainMenuBar = Application.CommandBars(strToolBarName)
Set objCommandBarComboBox = oMainMenuBar.Controls.Add(msoControlEdit)

With objCommandBarComboBox
.Caption = strCtrlCaption
.OnAction = "CheckInput"
End With

End Sub

Private Sub CheckInput()

Dim myCtrl As Office.CommandBarComboBox
Dim i As Long

Set myCtrl = Application.CommandBars(strToolBarName) _
.Controls(strCtrlCaption)

If Len(myCtrl.Text) = 6 Then
For i = 1 To 6
If Not Mid(myCtrl.Text, i, 1) Like "[0-9]" Then
MsgBox "You must type only numbers."
Exit Sub
End If
Next
MsgBox "You HAVE typed 6 numbers."
Else
MsgBox "You have NOT typed 6 numbers."
End If

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top