Consulting

Results 1 to 7 of 7

Thread: how to create new table feilds in VBA

  1. #1

    Cool how to create new table feilds in VBA

    ok, what im trying to do is create a VBA form front end that populates a word document.

    i know how to populate bookmarks and objects from a VBA Form.

    the problem is that i need to add an indeterminate amount of information.

    for example: lets say i need to add names to a list which i want to appear in a table in a word document. But i don't know how many names will be added.. How do i make it so that every time someone enters information in a VBA form it creates a new line in a table and automatically populates the data?

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    Hi dtrompstine,

    Each time you add a name on your userform, simply add a new row to the table using code like:
    [VBA]Sub AddRow()
    With ActiveDocument.Tables(1)
    .Rows.Add
    With .Rows.Last
    'modify the following lines to insert the values from your useform
    .Cells(1).Range.Text = "Hello World"
    .Cells(2).Range.Text = "The quick brown fox."
    End With
    End With
    End Sub
    [/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3

    Red face

    Thanks heaps for the reply.. thats awesome..

    just one more question.. what code would i use to create a checkbox in one of the cells and give it a lable?

    thanks again

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    Hi dtrompstine,

    Assuming you mean a checkbox formfield, plus some text:
    [vba]Sub AddRow()
    With ActiveDocument.Tables(1)
    .Rows.Add
    With .Rows.Last
    'modify the following lines to insert the values from your useform
    .Cells(1).Range.Text = "Hello World"
    With .Cells(2)
    .Range.FormFields.Add Range:=.Range, Type:=wdFieldFormCheckBox
    .Range.InsertAfter vbTab & "The quick brown fox"
    End With
    End With
    End With
    End Sub[/vba]Of course, for the checkbox formfield to work, the document will at some stage have to be protected for forms.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5

    Question

    cool... ok i have another question:

    i am writing a form which based on a particular selection from a combo box will have to launch another form... I know how to use if statments in order to launch the new form but.... what i would like to do is: once it has identified that it needs to launch the new form, i would like the code to wait untill the new form has been filled in and then continue running the code..... is that possible??

    does it even make sense?

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    In a sense it will do this.

    Userform1 has focus (it is running code).
    Logic determines userform1 needs to be activated.
    Userform2 activated. It now has focus. It will retain focus until you release it.
    When userform2 is released, focus is passed back to whatever called it, in this case, Userform1.

  7. #7
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    992
    Location
    ok i have another question:
    The way I do that is, say, you have a command button that calls userform 2.

    [vba]
    Private CommandButton1_Click()
    Userform2.Show
    'When Userform 2 is closed/unloaded, it will come back here.

    'Close Userform 1 or do other things.
    Unload Me
    End Sub
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •