Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controlling Word table column widths via Excel VBA

Status
Not open for further replies.

DJCNOR

Programmer
Sep 14, 2006
12
GB
Hello,

I have written(with help from this discussion group) a macro in Excel which directs the opening of a Word template document and the placement of a variety of information from an Excel workbook at bookmarks in that document.

At one of those bookmarks, a table is created, placed, and its cells filled with the proper information. (This cannot be done in the template because the number of rows in the table is not known until the macro counts non-blank rows in a worksheet.)

All this works fine. However, the table as formed has columns of equal width, but I need the columns to be different preset widths. I assume I want a Set statement, but I'm confused by all the layers of properties, I think.
For example, do the different table rows need to be named or can I just use numbers? And how do I specify the units of the width?

Set ActiveDocument.Tables("Tablename").Rows(1).Width=0.5 inches

That can't be right But what is?

DJC
 



Hi,

The macro recorder is your friend and tutor. In Word, turn on the macro recorder and change the column width.

Skip,

[glasses] [red][/red]
[tongue]
 
Hi, Skip,

My experience has been that what is recorded inside Word when you do something is not what works in a macro written to operate from Excel. None-the-less, I tried it, and it suggested something that seemed like it might work. This is what I added to the Excel macro where "Goods" is the name of the table and I intended to set the width for column 1:

Selection.Tables("Goods").Columns(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables("Goods").Columns(1).PreferredWidth = InchesToPoints(0.5)

When I tested it, I got an error message hilighting Selection and saying it was an "Invalid qualifier".
 



Do you have a reference set for MS Word n.m Object Library?

Skip,

[glasses] [red][/red]
[tongue]
 
Hi,

Just checked to make sure. Yes I do.

DJC
 


The inchestopoints method wants the appropriate application object
Code:
Selection.Tables("Goods").Columns(1).PreferredWidth = oWordAppl.InchesToPoints(0.5)
I'd also explicitly specify the object that the Table object is referencing.

Skip,

[glasses] [red][/red]
[tongue]
 
The first thing you said, I didn't understand. If it's lacking something, it was lacking that thing as "written" by the macro recorder in Word.

What's this oWordAppl bit?

Do you recommend substituting that one line for both my lines, then?

I tried adding "ActiveDocument." between "Selection." and "Table....". Same result.

DJC
 


a recorded macro is a starting point that must be taylored to your needs. Its much easier than starting from scratch.

"The inchestopoints method wants the appropriate application object" it's the WORD application object that you must Set using the CreateObject or GetObject method.




Skip,

[glasses] [red][/red]
[tongue]
 
Hi, Skip,

That was done long ago in my macro, before all the bookmark inserts were made and before the table was even created with

Set Wrd=CreateObject("Word.Application")

Now, I'm in the middle of a page worth of lines under

With Wrd

DJC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top