I need to introduce a title, some standard information, and a table into an existing Word document. This block of new data needs to be positioned before the existing text.
My attempt to code this is as follows:
Dim oCell As Word.Range
Dim oDoc As Word.Document
Dim oTable As Word.Table
' strTitle and strInformation are set here
Selection.TypeParagraph
Selection.Font.Size = 24
Selection.Font.Name = "Arial"
Selection.Font.Bold = True
Selection.Font.Color = RGB(18, 75, 122)
Selection.TypeText (strTitle)
Selection.Font.Size = 10
Selection.Font.Name = "Arial"
Selection.Font.Bold = True
Selection.Font.Color = RGB(0, 0, 0)
Selection.TypeParagraph
Selection.TypeText (strInformation)
Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=2)
With oTable.Range.Cells
.Height = 20
End With
With oTable.Range.Font
.Name = "Arial"
.Size = 8
End With
Set oCell = oTable.Cell(1, 1).Range
oCell.End = oCell.End - 1
oCell.Fields.Add oCell, Type:=wdFieldDocVariable, Text:="""Name01""", PreserveFormatting:=False
' Similar code exists for the other cells in the table, and is not repeated here. The variable 'Name01' is returning the correct value.
Selection.TypeParagraph
Selection.Font.Color = RGB(255, 0, 0)
Selection.Font.Bold = True
Selection.TypeText Text:="Original text starts here ..." 'Coloured red
' Existing text reverts back to non-bold black text. Remaining text is unchanged.
Selection.Font.Bold = False
Selection.Font.Color = RGB(0, 0, 0)
' --------------------------------------------------------------------------------------------------------------------------
The majority of the code is working OK. However, 2 problems remain.
1) I want the text in the cells to be aligned to centre and left.
2) The message "Original text starts here" is positioned in the table cell instead of in the body of the document.
Any help would be appreciated.