View Single Post
 
Old 10-15-2014, 09:27 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I agree with Graham. You don't need any bookmarks if you are writing data to table cell. However, as your objective tables (table 8-17) could be more than simply basic rows and columns and creating them with code can get gnarly very quickly, I would do it a little differently.

In your template create the first 7 tables and the first objective table (table 8). Select the objective table and create with it a building block. Call it "Objective Table" and save it to the template.

Then use code something like this:

Code:
Sub Example()
Dim AppWord As Object
Dim oDoc As Object
Dim oTbl As Object
Dim oRng As Object
Dim lngIndex As Long, lngObjectives As Long
  On Error Resume Next
  Set AppWord = GetObject(, "Word.Application")
  If Err Then
      Set AppWord = CreateObject("Word.Application")
  End If
  On Error GoTo 0
  Set oDoc = AppWord.Documents.Add("D:\Demo Template.dotm")
  'Write the first objective.
  Set oTbl = oDoc.Tables(8)
  With oTbl
    .Cell(2, 2).Range.Text = "Some text"
    .Cell(4, 1).Range.Text = "Some other text"
    'etc.
  End With
  'Are there other objectives? Yes in this demo 5
  lngObjectives = 5 'Replace with whatever you have now that determines the number of objectives required.
  For lngIndex = 2 To lngObjectives
    Set oRng = oTbl.Range
    oRng.Collapse wdCollapseEnd
    oRng.MoveEnd wdCharacter, 1
    oRng.InsertBefore vbCr
    oRng.MoveStart wdCharacter, 1
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Objective Table").Insert Where:=oRng
    'Write data to new objective as required.
    Set oTbl = oRng.Tables(1)
    With oTbl
      .Cell(2, 2).Range.Text = "Some text"
      .Cell(4, 1).Range.Text = "Some other text"
      'etc.
    End With
  Next lngIndex
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote