Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a WORD Table at a Bookmark

Status
Not open for further replies.

WBURKERT

Technical User
May 28, 2010
73
This is the code that is working for me right now. Before I Add a Table found at Bookmarks ("section_2") I would like to delete a table at Bookmarks ("section_2). It would be nice if no Table existed at Bookmarks ("section_2") that the code would just keep on executing and add a table at Bookmarks ("section_2").

Sub Section_X_to_Word_Table_at_Bookmark_X()

'WORD variables

Dim docDest As Word.Document
Dim Goods As Word.Table
Dim oCell As Word.Cell

'EXCEL variables

Dim appXL As Excel.Application
Dim rngWithData As Excel.Range
Dim mySpreadsheet As Excel.Workbook
Dim aryWithData, i As Integer, j As Integer

'row/column counters
Dim RowCount As Long
Dim ColCount As Long
'****************************************************************************************
'****************************************************************************************
Set appXL = CreateObject("Excel.Application")
Set mySpreadsheet = GetObject("F:\Documents\ACME\Sec2.xls")
Set rngWithData = mySpreadsheet.Worksheets(1).Range("Print_Area")

RowCount = rngWithData.Rows.Count
ColCount = rngWithData.Columns.Count

Set docDest = Documents.Open("F:\Documents\ACME\ACME LIST with TOC.doc")
Set Goods = docDest.Tables.Add(docDest.Bookmarks("Section_2").Range, RowCount, ColCount)
With Goods
'.Range.Style = "greenbar"
.Tables(1).Rows.Alignment = wdAlignRowCenter
.Columns(1).Width = InchesToPoints(1)
.Columns(2).Width = InchesToPoints(4)
.Columns(3).Width = InchesToPoints(1)
.Rows(1).Range.Font.Bold = True

End With
For Each oCell In Goods.Range.Cells
oCell.Range.Text = rngWithData.Cells(oCell.RowIndex, oCell.ColumnIndex).Value
Next

Set rngWithData = Nothing
Set Goods = Nothing

End Sub
 
...
Set docDest = Documents.Open("F:\Documents\ACME\ACME LIST with TOC.doc")
With docDest.Bookmarks("Section_2").Range.Tables
If .Count > 0 Then
.Item(1).Delete
End If
End With
Set Goods = docDest.Tables.Add(docDest.Bookmarks("Section_2").Range, RowCount, ColCount)
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
Set Goods = docDest.Tables.Add(docDest.Bookmarks("Section_2").Range, RowCount, ColCount)
that still puts the new table OUTSIDE the bookmark.

Therefore, if you execute this a second time:
Code:
With docDest.Bookmarks("Section_2").Range.Tables
  If .Count > 0 Then
    .Item(1).Delete
  End If
End With
it will return 0. In fact, unless you execute code to put the table INSIDE the bookmark in the first place:
Code:
With docDest.Bookmarks("Section_2").Range.Tables
  If .Count > 0 Then
will always return 0.

Gerry
 
In other words, if you want to test for a table in a bookmark, you had better be sure it is going IN the bookmark in the first place.

See the other thread. This can be done.

Gerry
 
Replace this:
Set Goods = docDest.Tables.Add(docDest.Bookmarks("Section_2").Range, RowCount, ColCount)
with this:
Set Goods = docDest.Bookmarks("Section_2").Range.Tables.Add(docDest.Bookmarks("Section_2").Range, RowCount, ColCount)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top