Results 1 to 15 of 15

Thread: How do I dynamically create textboxes in a custom word form - Resolved

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    How do I dynamically create textboxes in a custom word form - Resolved

    I have a customer form fired up from a word macro and I want to create a number of textboxes on it. I have to create them dynamically as I don't know how many at design time.

    My own investigations and a trawl round the forums pointed me at 2 possible ways of achieving it but neither's working for me:-

    1 txtBox = Me.Controls.Add(progid, "txtField", True) The problem here is I can't work out the progid for a text box - various sources quote VB.TextBox but word doesn't seem to recognise VB - maybe I just need to add a reference but I can't work out which.

    2 Create an control array with a single element and then 'load' it to create new instances of the control in the array, something like: Load txtField(1) The problem with this one is that word doesn't seem to support control arrays - the text box has no index property and if I try to add an identically named control it barfs and tells me the name's ambiguous, no option to create an array.

    So can anyone tell me how to:-
    get the progID in word for a text box
    or
    create a control array in word
    or
    do something else entirely?

    Thanks in advance
    Last edited by FunkyDexter; Jun 22nd, 2005 at 04:01 AM.

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

    Re: How do I dynamically create textboxes in a custom word form

    are you refering to a textbox as in a document frame or a text form field?

    if the latter,
    Add Method (FormFields Collection) Example

    This example adds a check box at the end of the selection, gives it a name, and then selects it.

    Selection.Collapse Direction:=wdCollapseEnd
    Set ffield = ActiveDocument.FormFields _
    .Add(Range:=Selection.Range, Type:=wdFieldFormCheckBox)
    With ffield
    .Name = "Check_Box_1"
    .CheckBox.Value = True
    End With
    from help

    pete

  3. #3

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form


    I'm not even sure what that meant. eeep!
    I think what that would do is put a textbox on my word document? If so it's not what I'm after I'm afraid.

    What I'm looking for is the equivalent of a text box on a normal vb form. In words vb editor insert a userform into your project and open up the toolbox. There's a textbox control (third control from left but that might be dynamic for all I know - it's called textbox) that you can drag onto your form and it'll create a textbox. That's what I want but I want to create it from code.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: How do I dynamically create textboxes in a custom word form

    Tengo mas preguntas que contestas

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

    Re: How do I dynamically create textboxes in a custom word form

    yes the code i posted puts a vb style textbox for a word form

    but if in word you goto menu> insert> textbox that is completely different

    pete

  6. #6

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form

    @westconn1, Yeah, that's not what I'm looking for I'm afraid - both are examples of putting 'textboxes' onto the word document itself whereas I want to put one onto a custom userform. The input wasn't wasted though as I think I'm going to want to do that soon anyway so thanks.

    @salvelinus, Yeah, I'd seen that thread but it actually didn't help much. In your post in that thread you declare the textbox but never instantiate it - I get a 'object not set' error if I do that. There's a link off the thread that shows you how to do the Load thing on a control array but that won't work because I can't create a control array. Finally, Silvrwood ends up putting a load of invisible textboxes on the form and dynamically making them visible - this is what I'm doing at the moment as a workaround but it's cludgy and I don't like it - surely you MUST be able to create them dynamically, you can in standard VB.

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

    Re: How do I dynamically create textboxes in a custom word form

    here is how to dynamically create text boxes on a userform

    VB Code:
    1. Dim Mytb As Control
    2.  
    3. Private Sub CommandButton1_Click()
    4.  
    5.     Set Mytb = Controls.Add(("Forms.textbox.1"), "textbox2", Visible)
    6.     Mytb.Left = 18
    7.     Mytb.Top = 100
    8.     Mytb.Width = 175
    9.     Mytb.Height = 20
    10.     Mytb.Text = "This is fun." & Mytb.Name
    11.    
    12. End Sub

    this is tested and working
    textbox2 is the name of the created textbox, change as required
    if you want to create a heap of them you can do it in a for next loop

    pete
    Last edited by westconn1; Jun 17th, 2005 at 09:15 AM.

  8. #8

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form

    Now that looks like what I'm after. BIG thanks

    I've tried it in my form and it runs with no errors but the textboxes don't actually appear. I've made sure visible is true and that the top and left properties would leave it in a visible part of the form . It has a height and width. I can't think of any other reason it wouldn't show up. Any thoughts?

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

    Re: How do I dynamically create textboxes in a custom word form

    it showed in my form ok when i was trying it, makes sure it isn't under the command button or something else

    pete

  10. #10

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form

    Actually, I've got a couple of frames on the form and it should appear in one of them. I wonder if it's hiding behind one of them. Anyway, I doubt I'll get time to experiment with it further today so I'll have another crack at after the weekend.

    Thanks for all your help, Westy

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

    Re: How do I dynamically create textboxes in a custom word form

    to put the textbox onto the frame you will have set its container
    if that exists in vba

    pete

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

    Re: How do I dynamically create textboxes in a custom word form

    turns out to be easy

    Set Mytb = Frame1.Controls.Add(("Forms.textbox.1"), "textbox" & i, Visible)

    remember that top is now measured in the frame

    pete

  13. #13

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form

    By the way, where did you find that ("Forms.textbox.1") value? I think it's basically the progID I was looking for in my first post isn't it?. I'm going to need to add other controls later and I could do with a reference.

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

    Re: How do I dynamically create textboxes in a custom word form

    i found it in the vba help file

    pete

  15. #15

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How do I dynamically create textboxes in a custom word form

    aha, found it. The machine I was working on today didn't have the help files installed so I was searching the net.

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