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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Word - Create Table

Status
Not open for further replies.

LJA520

Programmer
Nov 3, 2003
65
US
Hello,

In VBA - I am trying to create a table in a word document, and I'd like to move through the table to update the cells. Any idea how to do this? I'm a little lost.

Thanks in advance,
LJA520
 
Hve you tried Help? Look up Cell, look up Row, look up Table, and in particular look up Range. You do not say if you are selecting or using a range. You do not say if you updating text, or some sort of formula or field.

Gerry
 
Okay, I have the table created and updated the cells. My table is 3 columns by 4 rows. I'd like the 3rd column to right aligned. All I find is on row alignment and vertical alignment. Do you know how I would right align a column?

Thanks again!
LJA520
 
Have you tried the MacroRecorder ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Dpending on your version, I strongly suggest using Table styles. Once set up, you would never have to concern yourself with format again.

Gerry
 
Thanks for your help.

I figured it out using:

Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

Thanks,
LJA520
 
Yes, that would work, but it is a active process. You are using the Selection object - so the selection point has to BE in that cell to do the instruction.

Using Range, rather than Selection, would be better. it would be behind the scene.

Using a Table style would also not require the selection point to be in the cell.

Gerry
 
Hello Gerry,

I am inserting multiple tables on a word document (VBA). Each table consists of 6 rows each. When I print the pages, the contents of some tables are separated onto two pages. I tried setting "AllowPageBreaks" to false, but it is still happening. Is there anyway to keep the table together between pages. OR see what line I am on and insert a page break if necessary?

Thank you!!
LJA
 
What version?

When you set AllowPageBreaks to False, are you doing that for the entire table? It is a Row property.

Gerry
 
6.0

I set it false for the entire table and each row. Also, if I manually do it, it is still split.

Now I am trying to find out where I am on the page, do you know how to get the coordinates of the location. I have tried using the start property, but that is continuous for the whole document not each page. Any ideas??

Thanks.
 
Word 6.0......bummer. That was about the worst version of Word ever released.

I am not even sure...wait a sec...does not Word 6.0 use WordBasic, not VBA?

I know how to get the location number of the cursor in VBA, but darned if I can remember just about any syntax with WordBasic.

Gerry
 
Gerry, I'm sorry it is Word 2000, I was looking at the vba description. Can you tell me how to get the location number of cursor in vba? Thank you!
 
Oh, the cursor is the Selection object. It has a Start and End property in the form of a Long Integer. The beginning of the document is 0, and it counts every character (including spaces) from there. The Selection object can of course be more than one character long. When it is just one character, it can be said to have selected nothing...sort of. So when the cursor (the Selection) is "empty". the range Start and End are equivalent.

Msgbox Selection.Range.Start

will display the current location of the START of the selection.

If you want to make sure that the Selection is, in fact, collapsed to a point, look up Collapse in Help. You can collapse in either direction (forwards or backwards).

However, this is likely not the real solution to your problem. Are you absolutely sure that you have tried everything to make the table not break over a page? Is the table so big that HAS TO?

Oh,since you are thinking about playing with locations, you will most likely need to also look up "predefined bookmarks" in Help. For example, you can determine the current Start and End locations (as numbers, integers) of current page, using the "\page" bookmark. For example:

Code:
Sub CheckTable()
If Selection.Information(wdWithInTable) = True Then
  If Selection.Tables(1).Range.End > _
     ActiveDocument.Bookmarks("\page").Range.End Then
     MsgBox "This table extends beyond this page."
  Else
     MsgBox "This table does NOT extend beyond this page."
  End If
Else
  MsgBox "Selection is not in a table."
  Exit Sub
End If
End Sub

But this is only useful if the Selection is in fact in a table, a rather tedious exercise. yes, you could loop through all the tables in the document, and make the above a Boolean function, and if True (it DOES break across the page), put in a page break - however, you should be able to make a setting that disallows that, without code. Something is not quite right here.

Or is the table just too darn big?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top