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
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