[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Adjust columns in a Word table using VBA

Posted on 2011-02-19
2
Medium Priority
?
1,636 Views
Last Modified: 2012-05-11
    I am using VBA with Word 2010 and I want to adjust a seven-column table. I want to specify the widths of certain columns. Columns 1, 2 and 6 are hidden, so their width is of little importance, but I would like them to be as narrow as possible so that the visible columns have room to display things.

Visible columns:
     Column 3 contains nothing but a numbered list that Word generates to indicate a sequence number for each row. I need that to be just wide enough to display a three digit number that the computer generates.

     Columns 4 and 5 should be as wide as possible.
     Column 7 will contain a three-digit number, and I want it as narrow as possible so that columns 4 and 5 can be as wide as possible to display some text.

     I am trying to control the size of certain columns with variants of this line of code that the macro recorder generated:

          Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=3, RulerStyle:=wdAdjustNone

     As far as I can see, no matter what number I put after “ColumnWidth:=”, large or small,  I get the same error message: “Run-time error; Value out of range”.

     Can anyone help? To fill in the best values for the table, I would like to find out how wide the columns are when I adjust the table manually. However that is a different question, so I will save that for later.
     
     Thanks for your help.
     j.r allen
     Priddis, Alberta, Canada
0
Comment
Question by:JohnRobinAllen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 total points
ID: 34934933
Hi JRA,

Try it like this:
Sub TableWidths()
    Dim tbl As Table
    Set tbl = ActiveDocument.Tables(1)
    tbl.Columns.PreferredWidthType = wdPreferredWidthPoints
    tbl.Columns(1).PreferredWidth = CentimetersToPoints(1.8)
End Sub

Open in new window

0
 

Author Closing Comment

by:JohnRobinAllen
ID: 34937421
Few things in this world are perfect, but Graham Skan's solutions are always just that.

I, and many others, are grateful for his help and for this forum that lets him and others like him lead the masses out of the darkness into the promised land.

Thanks again!
    --j.r.a.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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.

656 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