0

I am trying to setup a table that embeds another table and fill it in from Excel (mailmerger). I am struggling to jump from cells to cells in Word, could someone give me some idea where to start?

 -------------------
 | Text in Cell1    |
 |------------------|
 |  --------------  |
 |  | Text newtbl | |
 |  |-------------| |
 |  |Text again   | |
 |  --------------  |
 --------------------

A code that I tried to combine but only add stuff in the first cell.

Sub test()
Dim objWord As Object 'a new instance of Word
    Dim objDoc As Object    'our new Word document


    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add(DocumentType:=0)
    objDoc.PageSetup.PageWidth = objWord.CentimetersToPoints(10.5)
    objDoc.PageSetup.PageWidth = objWord.CentimetersToPoints(14.8)

    objWord.Activate

    Dim objTbl1 As Object
    Set objTbl1 = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, NumRows:=2, NumColumns:=1)
    Set objRow1 = objTbl1.Rows(1)
    objRow1.Range.Text = "Feb 2019"
    Set objRow1 = objTbl1.Rows(2)
    Dim objTbl2 As Object
    Set objTbl2 = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range.InsertAfter, NumRows:=8, NumColumns:=1)
    Set objRow2 = objTbl2.Rows(1)
    objRow1.Range.Text = "Sunday"
    Set objRow2 = objTbl2.Rows(2)
    objRow1.Range.Text = " "
    Set objRow2 = objTbl2.Rows(3)
    objRow1.Range.Text = "Monday"
    Set objRow2 = objTbl2.Rows(4)
    objRow1.Range.Text = " "
    Set objRow2 = objTbl2.Rows(5)
    objRow1.Range.Text = "Tuesday"
    Set objRow2 = objTbl2.Rows(6)
    objRow1.Range.Text = " "
    Set objRow2 = objTbl2.Rows(7)
    objRow1.Range.Text = "Wednesday"
    Set objRow2 = objTbl2.Rows(8)
    objRow1.Range.Text = " "
End Sub
0

2 Answers 2

1

Please see this little experiment including Range, Row, Column and Cell objects:

Private Sub Test()
    Dim objword As Word.Application
    Dim objDoc As Word.Document
    Dim objRange As Word.Range
    Dim objTbl1 As Word.Table, objTbl2 As Word.Table
    Dim objRow As Word.Row
    Dim objCell As Word.Cell

    Set objword = CreateObject("Word.Application")
    objword.Visible = True
    Set objDoc = objword.Documents.Add(DocumentType:=0)
    objDoc.PageSetup.PageWidth = objword.CentimetersToPoints(10.5)
    objDoc.PageSetup.PageWidth = objword.CentimetersToPoints(14.8)

    objword.Activate

    Set objTbl1 = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, NumRows:=2, NumColumns:=1)

    ' Just to explain "For Each Cell":
    For Each objCell In objTbl1.Rows(1).Cells
        objCell.Range.Text = "Feb 2019"
    Next objCell

    ' Range (to be collapsed to add a larger table within cell)
    Set objRange = objTbl1.Columns(1).Cells(2).Range
    objRange.Collapse (wdCollapseStart)
    Set objTbl2 = objDoc.Tables.Add(Range:=objRange, NumRows:=8, NumColumns:=1)

    With objTbl2.Columns(1)
        .Cells(1).Range.Text = "Sunday"
        .Cells(2).Range.Text = "Monday"
    End With

End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for that I can't seem to be able to add the new doc command
I edited it to the full code. You also need a reference to "Microsoft Word x.x. Object Library" to allow early binding - which I did to allow better testing.
it works thanks the only thing I am confused about it the 3 row that I don't need
@TryHarder That's in the other answer only ... because he wrote NumRows:=3 :)
Always any kind of Range :) objTbl1.Range.Next(Unit:=wdParagraph, Count:=1).InsertBreak Type:=wdPageBreak and please consider to mark it as answer as explained here: stackoverflow.com/help/someone-answers
|
1

Why not try like This

Sub test()
Dim objWord As Word.Application 'a new instance of Word
Dim objDoc As Document    'our new Word document

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add(DocumentType:=0)
    objDoc.PageSetup.PageWidth = objWord.CentimetersToPoints(10.5)
    objDoc.PageSetup.PageWidth = objWord.CentimetersToPoints(14.8)

    objWord.Activate

    Dim objTbl1  As Table
    Set objTbl1 = objDoc.Tables.Add(Range:=objDoc.Paragraphs(1).Range, NumRows:=3, NumColumns:=1)
    objTbl1.Cell(1, 1).Range.Text = "Feb 2019"
    objTbl1.Cell(2, 1).Range.Text = " "
    Dim objTbl2 As Table
    Set objTbl2 = objTbl1.Cell(2, 1).Tables.Add(Range:=objTbl1.Cell(2, 1).Range, NumRows:=8, NumColumns:=1)

    For i = 1 To 8
    objTbl2.Cell(i, 1).Range.Text = "Day" & i
    Next i

End Sub

3 Comments

Thanks Ahmed, why do you need this: objTbl1.Cell(2, 1).Range.Text = " "?
@TryHarder Because it otherwise adds the second table with 1 cell only. Either you add text there to be replaced by table insertion or you collapse the cell's range as I did in my answer.
it works thanks the only thing I am confused about it the 3 row that I don't need, it creates an extra row at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.