Results 1 to 6 of 6

Thread: [RESOLVED] Macro in dotm template?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Macro in dotm template?

    Sub AutoNew()
    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    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


    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
    Last edited by Bockstensmannen; Apr 26th, 2013 at 04:25 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Macro in dotm template?

    try more like
    Code:
    me.controls("label" & nr).caption = "blah"
    'or
    set ctr = me.controls("label" & nr)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    7

    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

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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