title: Creating tables at bookmarks in a Word document through Excel VBA from dynamic controls
body: <p>I'm hoping someone can help me on this as I have been banging my head against this for about a week or so.</p>
  
  <p>I have a userform that creates a set of controls (3 text boxes and a combo box) and interates a counter by 1 each time the button is pressed.</p>
  
  <p>I have written an Excel VBA Macro that should open up a specific document (which will be a dotx in the final version but is a docx for this test) and;</p>
  
  <p>Go to a set bookmark in the Word Document ("table1")
  Move to the start of the line with the bookmark
  Move up to the previous line
  Create a table with 1 row and 3 columns.
  Inserts the values from the first and second textboxes and combobox into columns 1,2 and 3 respectively
  Return to the specified "table2" bookmark and repeat this for each iterated line of the dynamically created controls</p>
  
  <p>Also the Macro should</p>
  
  <p>Go to a set bookmark in the Word Document ("table2")
  Move to the start of the line with the bookmark
  Move up to the previous line
  Create a table with 1 row and 2 columns.
  Inserts the values from the first and third textbox into columns 1 and 2 respectively
  Return to the specified "table2" bookmark and repeat this for each iterated line of the dynamically created controls</p>
  
  <p>So basically at each bookmark if there are 3 lines of dynamic controls after the macro runs there should be </p>
  
  <p>A table with 3 rows and 3 columns located above bookmark "table1" containing the values from the first and second textbox and the combobox value.
  A table with 3 rows and 2 columns located above bookmark "table2" containing the values from the first and third textbox values.</p>
  
  <p>So my problem is that when the macro is run it either creates one line of the table at the top of the document, another at the "table1" bookmark and one at the "table2" bookmark or the 3 column table is created then another 3 column table is created inside the first cell of the first table and so on.</p>
  
  <p>I am convinced I am missing something in regards to cursor control (as the usual problem is the first table cell is created at the first line of the document then the cursor seems to move to the "table1" bookmark and the macro continues from there).</p>
  
  <p>If someone could give me some pointers on this I would greatly appreciated as I feel I am almost there but that I'm just overlooking something.</p>
  
  <p>Here is the code I have so far, apologies in advance for any unused variables this is back of a napkin coding.</p>
  
  <pre><code>Private Sub CommandButton14_Click() 'Create WO Letter
  'Open WO letter and copy paste data
  
      Dim objWord As Word.Application
      Dim objDoc As Word.Document
  
      Dim riskCombo As Control
      Dim theTextBox802 As Control
      Dim theTextBox803 As Control
      Dim theTextBox804 As Control
      Dim b As Integer
      Dim c As Integer
  
  Dim intNoOfColumns
  
  Dim wdDoc
  
  Dim objRange1
  
  Dim objRange2
  
  Dim objTable1
  
  Dim objTable2
  
      b = iRiskCount
      c = 1
  
  
      If Me.WOLetter1.Value = False And Me.WOLetter2.Value = False And Me.WOLetter3.Value = False And Me.WOLetter4.Value = False Then
          MsgBox "You Must Choose a Letter Type"
      Exit Sub
  
      End If
  
      If UserForm1.MultiPage1.Pages(2).Frame15.Controls("Risk" &amp; c).Value = "Risk" Then
           MsgBox "Select Risk Level for line " &amp; c
      Exit Sub
  
      End If
  
          If Me.WOLetter1.Value = True Then
                  Set objWord = New Word.Application
                  objWord.DisplayAlerts = False
                  objWord.Visible = True
                  objWord.Activate
                  Set wdDoc = objWord.Documents.Add(ActiveWorkbook.Path &amp; "\WOTest.docx")
  
  
          ElseIf Me.WOLetter2.Value = True Then
                  Set objWord = New Word.Application
                  objWord.DisplayAlerts = False
                  objWord.Visible = True
                  'objWord.Documents.Open ActiveWorkbook.Path &amp; "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
                  objWord.Activate
  
          ElseIf Me.WOLetter3.Value = True Then
                  Set objWord = New Word.Application
                  objWord.DisplayAlerts = False
                  objWord.Visible = True
                  'objWord.Documents.Open ActiveWorkbook.Path &amp; "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
                  objWord.Activate
  
          ElseIf Me.WOLetter4.Value = True Then
                  Set objWord = New Word.Application
                  objWord.DisplayAlerts = False
                  objWord.Visible = True
                  'objWord.Documents.Open ActiveWorkbook.Path &amp; "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
                  objWord.Activate
  
          End If
  
  
  
          For Each riskCombo In UserForm1.MultiPage1.Pages(2).Frame15.Controls
  
              If b &gt; 0 Then
  
              Set objRange1 = objWord.Selection.Range
              Set objRange2 = objWord.Selection.Range
  
      'Table1
              objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table1"
              objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
              objWord.Selection.MoveUp     
  
              wdDoc.Tables.Add objRange1, 1, 3
                  objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table1"
                  objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
              objWord.Selection.MoveUp
              Set objTable1 = wdDoc.Tables(1)
  
      'Table 2
  
              objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table2"
              objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
              objWord.Selection.MoveUp
  
              wdDoc.Tables.Add objRange2, 1, 2
                  objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table2"
                  objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
              objWord.Selection.MoveUp
              Set objTable2 = wdDoc.Tables(1)
  
              With objTable1
                  .Cell(0, 1).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text5" &amp; c).Value
                  .Cell(0, 2).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text6" &amp; c).Value
                  .Cell(0, 3).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Risk" &amp; c).Value
              End With
  
              With objTable2
                  .Cell(0, 1).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text5" &amp; c).Value
                  .Cell(0, 2).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text7" &amp; c).Value
              End With
  
  
                      c = c + 1
                      b = b - 1
  
          End If
  
      Next riskCombo
  
              objTable1.Columns(1).SetWidth ColumnWidth:=30, RulerStyle:= _
              wdAdjustNone
  
              objTable1.Columns(2).SetWidth ColumnWidth:=350, RulerStyle:= _
              wdAdjustNone
  
              objTable1.Columns(3).SetWidth ColumnWidth:=75, RulerStyle:= _
              wdAdjustNone
  
              objTable2.Columns(1).SetWidth ColumnWidth:=30, RulerStyle:= _
              wdAdjustNone
  
              objTable2.Columns(2).SetWidth ColumnWidth:=425, RulerStyle:= _
              wdAdjustNone
  
  
  End Sub 'end of test
  </code></pre>
  
  <p>Even some reassurance that I am on the right track with this would be appreciated as I have been beating my head against this for the past week.</p>