Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all,

How someone can help with this question. I am trying to write a number of tables to Word. I can create the document and application and add a table, my problem arises when I want to seperate them. I believe this is something to do with the "range". The first table is set at range (0, 0), I then try to reset the range, or try to insert some blank lines and reset the range. I have also tried to create a new range for each table but I get the same result everytime which is the new table gets inserted into the first cell.
I have done some serious brain damage reading MS help on ranges and how to reset them and also moving selections up on and down- nothing seem to work. Utimately I have 8 tables and I want one table on 8 seperate pages.
thanks for any help you can give


Imports Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices
Public junk As New DataTable
Private Sub WriteTables()
Dim oApp As Application
Dim oDoc As Document
'Start a new document in Word
oApp = CType(CreateObject("Word.Application"), Application)
oDoc = oApp.Documents.Add()
' Clear out any existing information.
' oDoc.Range.Delete()
' make the current documents the ActiveDocument
oDoc.Activate()


Dim rng As Range = oDoc.Range(0, 0)
rng.Font.Name = "Verdana"
rng.Font.Size = 16
Dim tlb As Table = oDoc.Tables.Add(Range:=rng, NumRows:= junk.Rows.Count, NumColumns:= junk.Columns.Count)
tlb.Range.Font.Name = "Arial"
tlb.Range.Font.Size = 20

With oApp.Selection
.SetRange(tlb.Range.End + 1, tlb.Range.End + 1)
.Collapse(WdCollapseDirection.wdCollapseEnd)
End With
' Dim i As Integer
' For i = 1 To 5
' oApp.Selection.MoveDown()
' Next


' rng = oApp.Selection.Range
' rng.SetRange(Start:=0, End:=8)
' rng.SetRange(Start:=rng.End + 3, End:=rng.End + 3)
Dim tlb1 As Table = oDoc.Tables.Add(Range:=rng, NumRows:=6, NumColumns:=6)
oApp.Visible = True

End Sub
Posted

1 solution

This page[^] shows how to add a new table at the end of a word document.

And also try adding a new paragraph for each new table - this VBA code creates a new document with two tables:

Sub Test()<br />    Dim NewDoc As Document<br />    Dim NewTable As Table<br />    Set NewDoc = Application.Documents.Add(, , , True)<br />    <br />    Set MyRange = NewDoc.Content<br />    MyRange.Collapse Direction:=wdCollapseEnd<br />    Set NewTable = NewDoc.Tables.Add(Range:=MyRange, NumRows:=6, NumColumns:=10)<br />    NewTable.Borders.OutsideLineStyle = wdLineStyleTriple<br />    <br />    NewDoc.Paragraphs.Add<br />    Set MyRange = NewDoc.Content<br />    MyRange.Collapse Direction:=wdCollapseEnd<br />    Set NewTable = NewDoc.Tables.Add(Range:=MyRange, NumRows:=6, NumColumns:=4)<br />    NewTable.Borders.OutsideLineStyle = wdLineStyleTriple<br />End Sub


 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900