Consulting

Results 1 to 7 of 7

Thread: Couple new by code created label with new by code created combobox

  1. #1
    VBAX Newbie
    Joined
    Jul 2017
    Location
    Tilburg
    Posts
    5
    Location

    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

  2. #2
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    What is the exact problem you have? Why are you having the code create the combo box versus just manually create the 2 items?

  3. #3
    VBAX Newbie
    Joined
    Jul 2017
    Location
    Tilburg
    Posts
    5
    Location
    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.

    vba coding -1.JPG

    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


  4. #4
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •