vb word center table

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.InsertParagraphAfter
    wdRange.Collapse wdCollapseEnd
    Set wdTable = .ActiveDocument.Tables.add(wdRange, 1, 3)
    wdTable.PreferredWidth = 600
jackjohnson44Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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")
    'or
    '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.InsertParagraphAfter
    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
    wdRow.Cells.Merge
    '...
    'wdDoc.Save
    'wdDoc.Close
    'wdApp.Quit
End Sub

jackjohnson44Author Commented:
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.
GrahamSkanRetiredCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.