View Full Version : Couple new by code created label with new by code created combobox
Goodmorning,
I have a little bit of a problem, first of all this is my first program in VB so i'm quite a noob.
i'm creating a combobox and label through vba coding by the clicking a button in word.
Now i want to couple (by code) the label and combobox so when these are created by clicking a button and the users selects something in the combobox this should appear in the label.
The reason is that the user can add extra steps in the document by clicking the button. Each step is label with combobox.
I'm using the following code to create an combobox and label (found on the internet and adapted a little bid)
one remark, Schakelacties_Stipel is an Array that i defined earlier.
'insert new combobox
Dim make_combobox As InlineShape
Set make_combobox = Selection.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
make_combobox.OLEFormat.Object.List = Schakelacties_Stipel
'insert new label
Dim lbl As InlineShape
Set lbl = Selection.InlineShapes.AddOLEControl _
(ClassType:="Forms.Label.1")
Thanks a lot!
With kind regards,
Row
heedaf
07-13-2017, 12:40 PM
What is the exact problem you have? Why are you having the code create the combo box versus just manually create the 2 items?
Hello Heedaf,
I'm creating an document where you can add a row to an table in Word. In each row of this table should be an label and a combobox. The combobox has a list with items/terms (in my case terms for switching high voltage, like open line etc). The user can select an item and then a predifined text has to be displayed in the label. For instance > selection cobobox: open line give's as result in a label: open the HV line at point:
This gives an plan how to switch high voltage gear according the official terms. The problem that i just cant place a couple of boxes is that each switching plan has never the same amount of steps, the one has 20 but another has 60... So my plan was to make an button on the document that add's an row with the label and combox.
My problem is that i'm creating a label and combobox but they are not linked together. I want to link the caption of the label to the combobox that are generated together (so each row has its own combobox and label and they are linked together, so when the user selects an item in the combobox a predifined text is shown in the label but all other boxes and labels are not changed). Below an example how it should be, note: row 16 is created by hand. row 17 is created by clicking "add step", it also creates label 11 and the combobox between the labeld and pushbutton.
Now i need to place the combobox and label at a fixed positon in a row and i need to link combobox to the label so it will show the predifined test.
19740
The code:
Private Sub Document_Open()
Call Gebruik_Array(Schakelacties_Stipel)
ComboBox1.List = Schakelacties_Stipel
End Sub
Private Sub ComboBox1_Change()
schakelactie = ComboBox1
Call Case_generator(schakelactie, definitie)
Label1 = definitie
End Sub
Private Sub Gebruik_Array(Schakelacties_Stipel)
Schakelacties_Stipel = Array("Inschakelen", "Uitschakelen", "Afschakelen", "Onder spanning brengen", "Vrijschakelen", "Koppelen", "Ontkoppelen", "Ring sluiten", "Ring openen", "Doorschakelen", "Verbreken", "Spanning testen", "Spanning meten", "Ontladen", "Aarden", "Kortsluiten", "Aarden en kortsluiten", "Zichtbaar aarden", "Doormeten", "Beproeven", "Aantonen v/d afwezigheid van bedrijfsspanning", "Scheiden", "Borgen", "Fasevergelijken", "Uitkleuren")
End Sub
Private Sub Case_generator(schakelactie, definitie)
Select Case schakelactie
Case "Inschakelen"
definitie = "Het inschakelen van"
Case "Uitschakelen"
definitie = "Het uitschakelen van"
Case "Afschakelen"
definitie = "Het afschakelen van"
Case "Onder spanning brengen"
definitie = "Het onder spanning brengen van"
Case "Vrijschakelen"
definitie = "Het vrijschakelen van"
Case "Koppelen"
definitie = "Het koppelen van"
Case "Ontkoppelen"
definitie = "Het ontkoppelen van"
Case Else
definitie = "niet gedefinieerd"
End Select
End Sub
Private Sub CommandButton1_Click()
Dim oTable As Table
Dim oCell As Cell
Dim oPrevRow As Row, oNewRow As Row
Dim iColumn As Long
Call Gebruik_Array(Schakelacties_Stipel)
' Insert new row
Set oTable = ActiveDocument.Tables(3)
Set oPrevRow = oTable.Rows(oTable.Rows.Count)
oTable.Rows.Add
Set oNewRow = oTable.Rows(oTable.Rows.Count)
' add a combobox
Dim New_combobox As InlineShape
Set New_combobox = Selection.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
New_combobox.OLEFormat.Object.List = Schakelacties_Stipel
' add a label
Dim New_label As InlineShape
Set New_label = Selection.InlineShapes.AddOLEControl _
(ClassType:="Forms.label.1")
New_label.OLEFormat.Object.Caption = "test"
End Sub
heedaf
07-14-2017, 09:52 AM
Why do they need to be linked? Or what exactly do you mean by "linked"? Within the code you should be able to update each at the same time so I don't really understand why linking would be required. If you want the combo box to update the label than add code to the after update option that will change the text of the label.
gmaxey
07-14-2017, 12:32 PM
Why does it matter why they need to be linked? It seems patently obvious from his description that "linked" means that when he selects something in Combobox1, the Label1 caption is set to a value defined in his code. If he adds a Combo/Label pair, he wants those to perform in a like manner. His problem seems to be that he doesn't have code to create the required to createCombobox2, 3, 4, 5 ... change events.
Row, ActiveX controls are a bit dated. If you want to go down this path, search for some help on creating a generic change event that is triggered when any combobox is changed. Then adapt your existing code to process the pair.
heedaf
07-14-2017, 01:02 PM
When I hear linked I think (in Access) that the label is assigned to a text/combo/check/etc box. Meaning that if you move one the other typically goes with it.
gmaxey
07-15-2017, 09:52 AM
You are going to need a class module name clsComboBox with the following code:
Option Explicit
Public WithEvents oComboBox As MSForms.ComboBox
Public Sub oComboBox_Change()
Dim oILS As InlineShape
Dim oLable As Object
For Each oILS In ActiveDocument.InlineShapes
If oILS.OLEFormat.Object.Name = "Label" & Right(oComboBox.Name, Len(oComboBox.Name) - 8) Then
Set oLable = oILS.OLEFormat.Object
Select Case oComboBox.Value
Case "Inschakelen": oLable.Caption = "Het inschakelen van"
Case "Uitschakelen": oLable.Caption = "Het uitschakelen van"
Case "Afschakelen": oLable.Caption = "Het afschakelen van"
Case "Onder spanning brengen": oLable.Caption = "Het onder spanning brengen van"
Case "Vrijschakelen": oLable.Caption = "Het vrijschakelen van"
Case "Koppelen": oLable.Caption = "Het koppelen van"
Case "Ontkoppelen": oLable.Caption = "Het ontkoppelen van"
'Finish this out as required.
Case Else: oLable.Caption = "niet gedefinieerd"
End Select
Exit For
End If
Next
lbl_Exit:
Set oILS = Nothing:
Exit Sub
End Sub
And then the following code in the ThisDocument module:
Option Explicit
Dim oDict As Object
Dim oEvent As clsComboBox
Private Sub Document_Open()
Gebruik_Array
ComboBox1.List = Gebruik_Array
GetEvents
lbl_Exit:
Exit Sub
End Sub
Sub GetEvents()
Dim oILS As InlineShape
Set oDict = CreateObject("Scripting.Dictionary")
For Each oILS In InlineShapes
If oILS.OLEFormat.ClassType = "Forms.ComboBox.1" Then
Set oEvent = New clsComboBox
Set oEvent.oComboBox = oILS.OLEFormat.Object
oDict.Add oILS.OLEFormat.Object.Name, oEvent
End If
Next oILS
lbl_Exit:
Exit Sub
End Sub
Private Function Gebruik_Array() As String()
Gebruik_Array = Split("Inschakelen,Uitschakelen,Afschakelen,Onder spanning brengen,Vrijschakelen,Koppelen," _
& "Ontkoppelen,Ring sluiten,Ring openen,Doorschakelen,Verbreken,Spanning testen,Spanning meten" _
& "Ontladen,Aarden,Kortsluiten,Aarden en kortsluiten,Zichtbaar aarden,Doormeten,Beproeven," _
& "Aantonen v/d afwezigheid van bedrijfsspanning,Scheiden,Borgen,Fasevergelijken,Uitkleuren", ",")
lbl_Exit:
Exit Function
End Function
Private Sub CommandButton1_Click()
Dim oTable As Table
Dim oRow As Row
Dim oCB As InlineShape, oLbl As InlineShape
'Insert new row
Set oTable = ActiveDocument.Tables(3)
o
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.