none
Adding a table in Word with Access VBA RRS feed

  • Question

  • Hi all,

    I am automating Word with MS Access VBA, which works fine.

    I now have a document to create. The first 2 paragraphs is normal text, which is no issue.

    After the text, I need to add a table. Here's the issue:

    When I add the table, it is displayed right at the top of the document, and not below the text where it should be.

    My Code:

        'Start Word and open the document template.
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        ''oRng.ParagraphFormat.SpaceAfter = 6
        ''Create Cover Page
        Set Dbs = CurrentDb
        'Get the company name
        Sql = "SELECT * FROM 01_Tbl_Company;"
        Set rst = Dbs.OpenRecordset(Sql)
        oRng.Paragraphs.Alignment = wdAlignParagraphCenter
        oRng.InsertAfter rst!CompanyName
        oRng.InsertParagraphAfter
        oRng.InsertAfter Forms!Frm_GenerateTest.Text2
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Final Assessment"
        
        Set oTable = oDoc.Tables.Add(oWord.Selection.Range, 1, 2)
        Set oRow = oTable.Rows.Add()
        oTable.Columns(1).Width = oWord.InchesToPoints(2)
        oTable.Columns(2).Width = oWord.InchesToPoints(1)
        oTable.Cell(1, 1).Range.Text = "Total Marks:"
        oTable.Cell(2, 1).Range.Text = "Duration:"
        oTable.Borders.Enable = True
    

    The Word document created:

    The table should be below the text "Final Assessment:"

    Much appreciated

    Deon

    Monday, December 18, 2017 8:58 AM

Answers

  • Hello Deon,

    You are adding the table at word application selection, so you need make work application select to the end of the paragraph. Please try to refer to below code.

    'move cursor to the end of the document
        oWord.Selection.EndKey Unit:=wdStory
        Set oTable = oDoc.Tables.Add(oWord.Selection.Range, 1, 2)
        'center the table in the document if you need
        'oTable.Rows.Alignment = wdAlignRowCenter

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Terry Xu - MSFT Tuesday, December 19, 2017 9:23 AM
    • Marked as answer by Deon SA Wednesday, December 20, 2017 5:28 AM
    Monday, December 18, 2017 9:56 AM
  • Hello Deon,

    Please re-set oRng object after adding table.

        ...
        oTable.Borders.Enable = True
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter
        ...

    Besides, please mark helpful answer to close thread if your original issue has been resolved and post a new thread if you have further requirements. Thanks for understanding.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Terry Xu - MSFT Tuesday, December 19, 2017 9:23 AM
    • Marked as answer by Deon SA Wednesday, December 20, 2017 5:29 AM
    Tuesday, December 19, 2017 9:22 AM

All replies

  • Hello Deon,

    You are adding the table at word application selection, so you need make work application select to the end of the paragraph. Please try to refer to below code.

    'move cursor to the end of the document
        oWord.Selection.EndKey Unit:=wdStory
        Set oTable = oDoc.Tables.Add(oWord.Selection.Range, 1, 2)
        'center the table in the document if you need
        'oTable.Rows.Alignment = wdAlignRowCenter

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Terry Xu - MSFT Tuesday, December 19, 2017 9:23 AM
    • Marked as answer by Deon SA Wednesday, December 20, 2017 5:28 AM
    Monday, December 18, 2017 9:56 AM
  • Thanks Terry for the prompt reply.

    Your suggestion works, but as soon as I add some more text to the document, the table created moves to the top of the second page.

    (Sorry, I am new to Word automation and it is a bit confusing at this stage).

    I attach my code again (more detail):

        'Start Word and open the document template.
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        ''Create Cover Page
        Set Dbs = CurrentDb
        'Get the company name
        Sql = "SELECT * FROM 01_Tbl_Company;"
        Set rst = Dbs.OpenRecordset(Sql)
        oRng.Paragraphs.Alignment = wdAlignParagraphCenter
        oRng.InsertAfter rst!CompanyName
        oRng.InsertParagraphAfter
        oRng.InsertAfter Forms!Frm_GenerateTest.Text2
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Final Assessment"
         
        oWord.Selection.EndKey Unit:=wdStory
        Set oTable = oDoc.Tables.Add(oWord.Selection.Range, 2, 2)
        oTable.Rows.Alignment = wdAlignRowCenter
        oTable.Columns(1).Width = oWord.InchesToPoints(2)
        oTable.Columns(2).Width = oWord.InchesToPoints(1)
        oTable.Cell(1, 1).Range.Text = "Total Marks:"
        oTable.Cell(2, 1).Range.Text = "Duration:"
        oTable.Borders.Enable = True
        oRng.InsertParagraphAfter
        oRng.InsertAfter "1. Cellular telephones and communication devices must be switched off and may not be used during an exam session."
        oRng.InsertAfter "2. Read and understand the questions carefully before answering. Them attempt to answer them all."
        oRng.InsertAfter "3. Ensure that you have your ID document or drivers license available for inspection."
        '------------------------------------------
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        ''================================================================
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.Paragraphs.Alignment = wdAlignParagraphLeft
        oRng.InsertAfter "Question Sheet for Course: " & Forms!Frm_GenerateTest.Text2
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Section A: True or False"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
    

    Thanks

    Deon

    Monday, December 18, 2017 10:59 AM
  • Hello Deon,

    Please re-set oRng object after adding table.

        ...
        oTable.Borders.Enable = True
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter
        ...

    Besides, please mark helpful answer to close thread if your original issue has been resolved and post a new thread if you have further requirements. Thanks for understanding.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Terry Xu - MSFT Tuesday, December 19, 2017 9:23 AM
    • Marked as answer by Deon SA Wednesday, December 20, 2017 5:29 AM
    Tuesday, December 19, 2017 9:22 AM
  • Thanks guys, all is working fine now.
    Wednesday, December 20, 2017 5:29 AM