PDA

View Full Version : Solved: !st row in a Table



MWE
02-15-2006, 08:16 PM
I have recently expanded my mismanagement of vba to Word and am having trouble with vba created tables.

After a little experimenting with table creation, I have discovered that EVERY TABLE regardless of # cols, #rows, has a messed up 1st row. In particular, the row is offset to the right from the rest of the table. The offset varies from doc to doc and position to position. The really frustrating part is that when the first row shifts to the right, it takes the right margin for the whole document with it.

An example of the code
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=10, _
NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed

Any idea what might be wrong

XLGibbs
02-15-2006, 09:10 PM
Well, from what I know, the table widths are usually identified by the contents within them..

Is there a way to more explicitly identify the destination top left position?

I know that cursor position makes a big difference when inserting those tables manually....

Bob Phillips
02-16-2006, 02:26 AM
I tried it and it looked perfectly normal to me. Can you post an image?

TonyJollans
02-16-2006, 05:12 AM
What version of Word are you using?

MWE
02-16-2006, 06:30 AM
What version of Word are you using?
I am using Word2000.

This problem is only when creating tables with VBA. Table creation manually works fine.

I also ran a (vba) test where the first row is deleted and the system pauses so I can see the results. The loop continues until there are no rows left. Each time, the new Row(1) is offset to the right. It is as though there is something else in that first row "space" like omnipotent formating. Yet when I create a table manually at the same location, everything is fine.

TonyJollans
02-16-2006, 06:47 AM
I wondered whether a Table Style might be causing the problem - but not in 2000.

It seems very odd. There is nothing obviously wrong with the code. Is there anything in the Selection when you do it? What Style are you using?

Can you post a sample document where it happens - or has happened.

MWE
02-16-2006, 08:19 AM
I wondered whether a Table Style might be causing the problem - but not in 2000.

It seems very odd. There is nothing obviously wrong with the code. Is there anything in the Selection when you do it? What Style are you using?

Can you post a sample document where it happens - or has happened.Tony: thanks for you help.

It gets even stranger. I stripped out everything from the VBA except the "make table" piece and then tried it in a number of documents and locations in those document. Row1 is always offset, but in some cases it is offset to the right (most cases) and in others it is offset to the left. So, this must be a formatting or style issue. Yet, manual table creation at the same insert point seems to work fine :dunno

I have attached a sample document. It has only two macros, MakeTable and DeleteTable. Both have most of their original code stripped out, but demonstrate the problem. I wrote the delete table proc because in the real appl, the table created could be several hundred rows across many pages. As I was trying to debug the table creation process, it was a pain to select everything and delete it

Tommy
02-16-2006, 08:53 AM
Hi MWE,

In the TestDoc change:
.Rows(1).Alignment = wdAlignRowCenter
To:
.Rows(1).Range.Paragraphs.Alignment = wdAlignParagraphCenter

HTH
Tommy

TonyJollans
02-16-2006, 09:15 AM
It's easy when you see it :)

After creation, you explicitly set the widths of each column - and the total width of the table becomes less than the page width - while the table remains, by default, left aligned.

Then, later, you do .Rows(1).Alignment = wdAlignRowCenter which aligns the top row to the centre of the page - hence offset from the left-aligned rest of the table.

MWE
02-16-2006, 01:01 PM
It's easy when you see it :)

After creation, you explicitly set the widths of each column - and the total width of the table becomes less than the page width - while the table remains, by default, left aligned.

Then, later, you do .Rows(1).Alignment = wdAlignRowCenter which aligns the top row to the centre of the page - hence offset from the left-aligned rest of the table.
It's just not fair :banghead: :banghead:I could swear that when I tested the code earlier I stripped out everything, including all the centering code and still had the problem. :banghead::banghead:I added some back for the example posted here.

It works. Many thanks. This also explains a problem I was having with general text centering in cells.

Actually, this was a test to see who understood tables in word (don't believe that huh?). So now that I have found someone, I know your name, you can not escape :devil:

Last question (for now):
I wish the first two rows in the table to be the "Header" and be repeated at the top of each page. It is pretty clear how to make one row the header, but how do I make two rows the header?

TonyJollans
02-16-2006, 02:05 PM
No!! Trapped in a Table forever!!:reading:

If you make the second row a heading row - the first will come with it ...

DocumentRef.Tables(1).Rows(2).HeadingFormat = True

geekgirlau
02-16-2006, 04:36 PM
Personally I have found that in my ongoing battle with Word tables, the simplest approach is to create a standard table as an AutoText entry, with headers, borders, shading column widths, paragraph alignment ad infinitum ...

TonyJollans
02-16-2006, 05:34 PM
Personally I have found that in my ongoing battle with Word tables, the simplest approach is to create a standard table as an AutoText entry, with headers, borders, shading column widths, paragraph alignment ad infinitum ...

You mean like a Table Style ought to be? :)

Hopefully Office 2007 will make them better. Till then you're probably right.

MWE
02-16-2006, 06:47 PM
Personally I have found that in my ongoing battle with Word tables, the simplest approach is to create a standard table as an AutoText entry, with headers, borders, shading column widths, paragraph alignment ad infinitum ...
Thanks. An interesting approach. That could work quite well for the target application.

And thanks for letting me now that WORD TABLES ARE A PAIN !

MWE
02-16-2006, 06:52 PM
You mean like a Table Style ought to be? :)

Hopefully Office 2007 will make them better. Till then you're probably right.
Why do I worry that Word 2007 will continue along the path of more useless features, more bugs, more glitz, and more frustration. With each new release of MS Offensive I spend hours trying to figure out how to make it work like version 4. I must ask Babydum about this

Thanks for all of you help (and do not try to hide :hide:-- we have ways of finding you)