?
Solved

vb word center table

Posted on 2006-04-24
3
Medium Priority
?
244 Views
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.InsertParagraphAfter
    wdRange.Collapse wdCollapseEnd
    Set wdTable = .ActiveDocument.Tables.add(wdRange, 1, 3)
    wdTable.PreferredWidth = 600
0
Comment
Question by:jackjohnson44
  • 2
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16530170
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
 

Author Comment

by:jackjohnson44
ID: 16534258
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 16537139
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Suggested Courses

850 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