[RESOLVED] Macro in dotm template?
Hi
I want to fill a bunch of comboboxes situated in a table in a word doc using a macro stored in a special dotm template.
So I name the macro AutoNew for it to be executed at start after a new doc is loaded.
When storing in a docm everything works like it should, but when storing in a dotm I can not grab the combobox.additem function or combobox.items.add... they are just not there, so when running the script it halts at the first fill combobox.
The only things I can grab here is settings for the ThisDocument.ComboBox1.BackColor :(
Same thing if I use
ThisDocument. infront of the comboboxes..etc
also tried
combobox1.items.add("sdfsdfd")
Sub AutoNew()
'
' AutoNew Makro
'
Dim Produkter(13) As String
Produkter(0) = ""
Produkter(1) = "GUNGSTÄLLNING"
Produkter(2) = "LEKSTÄLLNING"
Produkter(3) = "KLÄTTERSTÄLLNING"
Produkter(4) = "LEKHUS"
Produkter(5) = "STAKET"
Produkter(6) = "BRUNNSLOCK"
Produkter(7) = "RUTSCHKANA"
Produkter(8) = "FJÄDERGUNGA"
Produkter(9) = "VIPPGUNGA"
Produkter(10) = "VOLTRÄCKE"
Produkter(11) = "SANDLÅDA"
Produkter(12) = "FOTBOLLSMÅL"
Produkter(13) = "LINBANA"
Dim lngPosition As Integer
For lngPosition = LBound(Produkter) To UBound(Produkter)
ComboBox1.AddItem (CStr(Produkter(lngPosition)))
ComboBox2.AddItem (CStr(Produkter(lngPosition)))
ComboBox3.AddItem (CStr(Produkter(lngPosition)))
ComboBox4.AddItem (CStr(Produkter(lngPosition)))
ComboBox5.AddItem (CStr(Produkter(lngPosition)))
ComboBox6.AddItem (CStr(Produkter(lngPosition)))
ComboBox7.AddItem (CStr(Produkter(lngPosition)))
ComboBox8.AddItem (CStr(Produkter(lngPosition)))
ComboBox9.AddItem (CStr(Produkter(lngPosition)))
ComboBox10.AddItem (CStr(Produkter(lngPosition)))
ComboBox11.AddItem (CStr(Produkter(lngPosition)))
Next lngPosition
End Sub
Re: Macro in dotm template?
does this macro run at all, when a new document is opened based on this template?
i would believe that the code should be in the New event in the thisdocument code pane, use the drop down boxes at the top of the thisdocument code pane, to make sure you are in the correct place
Re: Macro in dotm template?
Indeed, it worked better wit the code in thisDocument>New Doc Thanks :)
Anyhow I have a wondering about dynamic Control-names, as there will be alot of code and hardcoding the controlnames. I am not so used to word programming more java etc but hopefully one can use "alias" variables for controls right?
As I have a relation Combobox1 should change Label1.caption when changed this seemed to be a good idea. but no luck here , nothing seems to work :mad:
my errous try
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
Re: [RESOLVED] Macro in dotm template?
try more like
Code:
me.controls("label" & nr).caption = "blah"
'or
set ctr = me.controls("label" & nr)
Re: [RESOLVED] Macro in dotm template?
There seems to be some Office or Word oriented programming-referencing problem. If I use the code in a Userform it works great in the same VBA Project, 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
Re: [RESOLVED] Macro in dotm template?
controls within a document are contained in shapes or inline shapes and the code has to be changed to address the control object within the named (inline)shape