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?
 
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.
0
 
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

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.