VBA Word - New text at start of document

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.









Answer
Answer

You incorrectly set the value of oTable. I have highlighted the required changes in bold text.

Get into the habit of declaring variables. Check VBA > Tools > Options > Editor > Require Variable Declaration. This will add Option Explicit to the start of your new modules and this force you to declare the variables.

Option Explicit

Sub MyTest()

Const strTitle As String = "This is the title"
Const strInformation As String = "This is the info line"
Const strVersionDate As String = "Version Date"
Const strVersionNumber As String = "99"
Const strReviewer As String = "A N Other"
Const strApprover As String = "The Publication Editor"
Const strUnchanged As String = "Original text starts here ..."
Dim oDoc As Document
Dim oRange As Range
Dim oTable As Table
Dim oCell As Range
    ' -----------------------------------------------------------------------------
    ' Document title and information

    Set oDoc = ActiveDocument
    Set oRange = oDoc.Range
    oRange.Collapse wdCollapseStart
    oRange.InsertParagraphAfter

    With oRange
        .Text = strTitle & vbCr
        .Font.Size = 24
        .Font.name = "Arial"
        .Font.Bold = True
        .Font.Color = RGB(18, 75, 122)
        .Collapse wdCollapseEnd

        .Text = strInformation & vbCr
        .Font.Size = 10
        .Font.name = "Arial"
        .Font.Bold = True
        .Font.Color = RGB(0, 0, 0)
        .Collapse wdCollapseEnd
    End With

    ' -----------------------------------------------------------------------------
    ' Document table

    'GoTo SkipTable:
    Set oTable = ActiveDocument.Tables.Add(Range:=oRange, NumRows:=2, NumColumns:=2)

    With oTable.Range
        .Cells.Height = 20
        .Cells.VerticalAlignment = wdCellAlignVerticalCenter
        .Font.name = "Arial"
        .Font.Size = 10
    End With

    With oTable.Borders
        .InsideColor = wdColorAutomatic
        .InsideLineStyle = wdLineStyleSingle
        .OutsideColor = wdColorAutomatic
        .OutsideLineStyle = wdLineStyleSingle
    End With

    ActiveDocument.Variables("varName01").Value = strVersionDate
    ActiveDocument.Variables("varName02").Value = strVersionNumber
    ActiveDocument.Variables("varName03").Value = strReviewer
    ActiveDocument.Variables("varName04").Value = strApprover
    ActiveDocument.Fields.Update

    Set oCell = oTable.Cell(1, 1).Range
    oCell.End = oCell.End - 1
    oCell.Fields.Add oCell, Type:=wdFieldDocVariable, Text:="""varName01""", PreserveFormatting:=False

    Set oCell = oTable.Cell(1, 2).Range
    oCell.End = oCell.End - 1
    oCell.Fields.Add oCell, Type:=wdFieldDocVariable, Text:="""varName02""", PreserveFormatting:=False

    Set oCell = oTable.Cell(2, 1).Range
    oCell.End = oCell.End - 1
    oCell.Fields.Add oCell, Type:=wdFieldDocVariable, Text:="""varName03""", PreserveFormatting:=False

    Set oCell = oTable.Cell(2, 2).Range
    oCell.End = oCell.End - 1
    oCell.Fields.Add oCell, Type:=wdFieldDocVariable, Text:="""varName04""", PreserveFormatting:=False

    oRange.Start = oTable.Range.End
    oRange.Collapse wdCollapseEnd

SkipTable:
    ' -----------------------------------------------------------------------------
    ' Document original text

    oRange.Text = strUnchanged
    oRange.Font.Color = RGB(255, 0, 0)
    
    Set oRange = Nothing
    Set oCell = Nothing
    Set oTable = Nothing
    Set oDoc = Nothing
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 827 Applies to: