I have a program that creates a report in MS Word 2010 from a database.
For each entry in the database it formats the data into a table in the word document.
However the size of the table can vary if one entry has extraordinarily large field (some 'Memo' fields) it can break a table and split it across 2 pages.
I want at the top of each page a common title (I realise this may be better using header/footers), i then want to start adding tables until one is split, if this happens insert a page break, another title and carry on until the end.
I have tried:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oPara1 As Word.Paragraph
Dim Pos As Double
Dim oRng As Word.Range
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oDoc.PageSetup.LeftMargin = oWord.Application.CentimetersToPoints(1.5)
oDoc.PageSetup.RightMargin = oWord.Application.CentimetersToPoints(1.5)
oDoc.PageSetup.TopMargin = oWord.Application.CentimetersToPoints(1.5)
oDoc.PageSetup.BottomMargin = oWord.Application.CentimetersToPoints(1.5)
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "Page Title"
oPara1.Range.Font.Bold = True
oPara1.Range.Font.Underline = True
oPara1.Range.Font.Size = "16"
oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
oPara1.Format.SpaceAfter = 6
Pos = oWord.CentimetersToPoints(23)
oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
Do
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
Dim oTable As Word.Table
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 7, 4)
oTable.Range.ParagraphFormat.SpaceAfter = 6
oTable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
'----- ADD TABLE DATA -----
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oTable.Rows.AllowBreakAcrossPages = False
' oPara1.Format.SpaceAfter = 6
'oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertAfter("Page Title")
oRng.Font.Bold = True
oRng.Font.Underline = True
oRng.Font.Size = "16"
oRng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
oRng.ParagraphFormat.SpaceAfter = 6
The do loop sort of works if the table falls after a set position but they may never do that as the overall table height could vary (although I must point out that I envisage a single table should never get to a position where it is bigger than 1 page!)