View Single Post
 
Old 10-15-2014, 07:24 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Exclamation

Rather than delete unwanted tables from the document, why not add them only when required? If you are populating cells of a table from your userform you don't need bookmarks to take the data you can address the table and cell(s) directly e.g.

Code:
Sub Example()
Dim AppWord As Object
Dim WordDoc As Object
Dim oTable As Object
Dim oRng As Object
Dim oCell As Object
Dim ExcSheet As Excel.Worksheet
Dim i As Integer

    On Error Resume Next
    Set AppWord = GetObject(, "Word.Application")
    If Err Then
        Set AppWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    AppWord.Visible = True
    'Set WordDoc = AppWord.Documents.Add("C:\Path\DocName.docx")
    Set WordDoc = AppWord.activedocument
    'Set a range to the table before the place to insert a new table
    Set oRng = WordDoc.Tables(7).Range
    'collapse the range to its end
    oRng.collapse 0
    'Add an empty paragraph to the range
    oRng.Text = vbCr
    'Collapse the range to the end of the empty paragraph
    oRng.collapse 0
    'and add a table at the range
    Set oTable = WordDoc.Tables.Add(Range:=oRng, NumRows:=2, NumColumns:=3)
    'Now fill the table
    Set oCell = oTable.Cell(1, 1).Range        'Row,Column
    oCell.End = oCell.End - 1
    oCell.Text = "Value for Cell 1,1"
    Set oCell = oTable.Cell(1, 2).Range
    oCell.End = oCell.End - 1
    oCell.Text = "Value for Cell 1,2"
    'Set oRng to oTable.Range
    'and repeat the above section for each new table depending on the coindition that determines whether a table is required.
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote