|
-
Apr 26th, 2013, 05:45 AM
#1
Thread Starter
New Member
[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.
-
May 2nd, 2013, 05:27 AM
#2
Thread Starter
New Member
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
-
May 4th, 2013, 10:11 PM
#3
New Member
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.
-
May 6th, 2013, 02:52 AM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|