Share via

Creating textboxes dynamically and placing the text in bookmarks

Anonymous
2021-08-08T00:16:37+00:00

Good day. Let me start with a disclaimer: I am not a programmer by training and so any help that members of this community can provide would be appreciated. I have created a user form in Word which will be used to generate terms and conditions for customer's orders. There are some situations where there are several customers under the same ticket and so I have added a button which would enable the user to add additional textboxes to input additional names. The code for adding new textboxes is below. Once those textboxes are created on the form, I want do the following things: (1) set focus to the box just added; (2) restrict the creation of new boxes if any of the existing textboxes are still empty; (3) for each textbox, add a sequential bookmark using the name of that textbox and finally (4) capture the information entered in each textbox, (5) place it in the corresponding bookmark and then insert each bookmark with the captured text in the document. I can't seem to come up with the code to do this. Any help will be appreciated. Thanks.

This is the code that i use to add the new textboxes:

Dim num As Long

Private Sub cmd_Add_Click()

Static i As Integer

Dim bm As Long

Dim strInput As String

Dim txtbox As Object

 Set txtbox = UserForm2.Controls.Add("Forms.TextBox.1") 

i = i + 1

num = i

         With txtbox 

        .Name = "Licensee" & i 

        .Left = 10 

        .Height = 20 

        .Width = 150 

        .Top = 20 + (30 \* i) 

    End With 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-08-08T01:21:47+00:00

    Many thanks. I will explore this.

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2021-08-08T01:10:59+00:00

    I would suggest having one Textbox on the UserForm and a LIstbox and a Command Button. Then when you have added the customer details into the Textbox, you would click on the Command button which would have the code to add that customer to the Listbox. When all customers have been added to the listbox, another Command Button would be used to transfer the data to the document, probably best done by the use of Document Variables.

    See the following pages of Greg Maxey's website :

    http://gregmaxey.mvps.org/Create\_and\_employ\_a\_UserForm.htm

    Was this answer helpful?

    0 comments No comments