Results 1 to 4 of 4

Thread: [RESOLVED] Dynamic Control-names in dotm or docm macros

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    Resolved [RESOLVED] Dynamic Control-names in dotm or docm macros

    I have a question about dynamic Control-names in a Dotm or Docm (template w macro or document w macro)
    as there will be alot of code, if hardcoding the controlnames.
    I am not so used to word programming more java etc but hopefully one can do the same in word VBA and use "alias" variables for controls right?

    As I have a relation: Combobox1 should change Label1.caption when changed, Combobox2 should change Label2.caption when changed
    This seemed to be a good idea. But no luck here , nothing seems to work

    my erroneous tries:

    Code:
    Public Sub ComboBox1_Change()
    DoLabel ("1")
    End Sub
    
    
    
    
    Public Sub DoLabel(ByVal nr As String)
    MsgBox (nr)
    Dim ctr As Control, nameStr As String
    
    'ctr= "Label" & nr
    
    ctr.Name = "Label" & nr
    
    Me.ctr.Caption = "Show this text"
    
    'Also tried 
    
    Me.ctr."Label" & nr.Caption="show this text"
    
    namestr="Label"&nr 
    'me.namestr.Caption="show this text"
    
    End Sub
    Last edited by Bockstensmannen; Apr 26th, 2013 at 08:01 AM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    Re: Dynamic Control-names in dotm or docm macros

    Halfway there...there seems to be some Office or Word oriented programming-referencing problem. If I use this code in a Userform in a VBA project in MS word 2007 it works great, but when having the code and the controls in the document ThisDocument I can not reference "Controls"...
    In the project if I go to ThisDocument >Code> then write "me." I wont get the "Controls" suggestion like when in a UserForm. I do get the individual control-names up though as suggestion "me.ComboBox1" for example.
    Any ideas?

    Thanks/Martin
    Code:

    Private Sub ComboBox1_Change()
    doLabel ("1")
    End Sub


    Private Function doLabel(ByVal nr As String)
    Me.Controls("TextBox" & nr).Text = "blah"

    'Tried ThisDocument.Controls("TextBox" & nr).Text = "blah"
    'Tried Project.ThisDocument.Controls("TextBox" & nr).Text = "blah"
    End Function

  3. #3
    New Member
    Join Date
    May 2013
    Posts
    2

    Re: Dynamic Control-names in dotm or docm macros

    Word contains some controls in a inlineshapes collection. Try something like this.


    Private Sub DoTextBox(nr as integer)

    Dim ctr as InlineShape
    For Each ctr in ActiveDocument.InlineShapes
    If ctr.OLEFormat.Object.Name = "TextBox" & nr Then
    ctr.OLEFormat.Object.Text = "Blah"
    End If
    Next

    End Sub


    Might need to test to see if the Object can support 'name', but this should get you pointed in the right direction.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    Re: Dynamic Control-names in dotm or docm macros

    Thanks guys! the Inlineshapes way was the way to go
    Before realising that I gave up the active-x controls and took a dive into Microsofts Contentcontrols whick I managed to grab easier, but then I insted realised Micorsoft had forgotten to add eventhandlers like combobox_changed and other important stuff like that.
    Whats the point with Comboboxes with no eventhandlers? way to go MS

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width