vb word center table

Posted on 2006-04-24
Last Modified: 2008-03-04
I have a vb app that writes to a word document.
I make a table in it and for some reason, every time I give the table a width, it is pushed way over to the right.
I want to center the table.

Also, how do you change the table so that the lines appear when printing?

    Set wdRange = .ActiveDocument.Bookmarks("myBookMark").Range
    .ActiveDocument.Bookmarks.add "myBookMark", wdRange 'readd bookmark
    wdRange.Collapse wdCollapseEnd
    Set wdTable = .ActiveDocument.Tables.add(wdRange, 1, 3)
    wdTable.PreferredWidth = 600
Question by:jackjohnson44
    LVL 76

    Expert Comment

    This will set the borders (lines).

    It also sets the width of each column to reduce the width of the table, and then centres the table horizontally.

    Sub AddTableAddRow()
        Dim wdApp As Word.Application
        Dim wdDoc As Word.Document
        Dim wdBmk As Word.Bookmark
        Dim wdRange As Word.Range
        Dim wdTable As Word.Table
        Dim wdRow As Word.Row
        Dim b As Integer
        Dim c As Integer
        Set wdApp = Application
        wdApp.Visible = True 'advised while developing, lest macro aborts, leaving an invisible application
        Set wdDoc = ActiveDocument
        Set wdDoc = wdApp.Documents.Add("MyTemplate")
        'Set wdDoc = wdApp.Documents.Open("C:\MyPath\MyDocument.doc")
        Set wdRange = wdDoc.Bookmarks("mybookmark").Range
        wdRange.Text = "type this text"
        wdDoc.Bookmarks.Add "mybookmark", wdRange 'readd bookmark in case it was overwritten
        wdRange.Collapse wdCollapseEnd
        Set wdTable = wdDoc.Tables.Add(wdRange, 1, 3)
        'set borders
        For b = wdBorderVertical To wdBorderTop
            wdTable.Borders(b).LineStyle = wdLineStyleSingle
        Next b
        'reduce width and centre the table
        For c = 1 To 3
            wdTable.Columns(c).Width = CentimetersToPoints(2.5)
        Next c
        wdTable.Rows.Alignment = wdAlignParagraphCenter
        'append a row and merge all its cells
        Set wdRow = wdTable.Rows.Add 'add one row
    End Sub


    Author Comment

    Thanks for the help, but I am still having problems.
    I would like the table to be a certain width, and I would like the text to wrap if it is too long.
    For some reason most of it wraps, but not all of it.
    Some characters are getting cut off, but when I drag the table side over, I can see everything.
    LVL 76

    Accepted Solution

    I can't reproduce the symptoms as you describe them, but there are some settings that might be relevent.

    These properties affect the column width
    Table.AutoFitBehaviour = |wdAutoFitWindow , wdAutoFitContent or wdAutoFitFixed|
    You can also set
    Table.AllowAutoFit  = |True or False|
    These setting interact to prevent them being in contradiction.

    You can also fix the height or allow the height to expand

    |Row, Cell, Rows or Cells|.HeightRule = |wdRowHeightExactly, wdRowHeightAtLeast or wdRowHeightAuto|

    If you fix both the height and width, the text could overflow the cell.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
    This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
    In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
    This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now