PDA

View Full Version : Solved: Create a Fixed Table



asugianto
05-02-2005, 11:54 AM
Hi Guys,

How do I create a fixed table in Word? Meaning that when I enter information, the table does not add the next row automatically.

Thanks!

MOS MASTER
05-02-2005, 12:03 PM
Hi, :D

Usually the next row only get's added when you press the "Tab" button in the last row (Cell)
Then a next row is added.

Is this the behaviour you want to stop? :whistle:

asugianto
05-02-2005, 02:11 PM
Hi Joost,

That would be correct.

Thanks!

MOS MASTER
05-03-2005, 10:34 AM
Hi asugianto, :D

Ah ok.
This is one off those things that you can't do without code.

You can use a buildin Wordmacro: "NextCell" that is fired when you move through a table with the "Tab-key."

Now you only have to check where you are in the table and you've got it working.

I've programmed the sub so that it goes round and round in the table and never leave it? (While using the "Tab-key")

I've also added a sub that will create a table. There are many way's to do this but mine write a table to a bookmark range.

The code:
Option Explicit

Sub NextCell()
Dim iRangeColNum As Integer
Dim iMaxNumCol As Integer
Dim iEndRangeRowNum As Integer
Dim iMaxNumRow As Integer

iRangeColNum = Selection.Information(wdEndOfRangeColumnNumber)
iMaxNumCol = Selection.Information(wdMaximumNumberOfColumns)
iEndRangeRowNum = Selection.Information(wdEndOfRangeRowNumber)
iMaxNumRow = Selection.Information(wdMaximumNumberOfRows)

If iRangeColNum < iMaxNumCol Or _
iEndRangeRowNum < iMaxNumRow Then

Selection.MoveRight Unit:=wdCell, _
Count:=1, _
Extend:=wdMove
Else
Selection.Tables(1).Cell(1, 1).Select
End If

End Sub

Sub AddATable()
Dim oRange As Word.Range
Dim oTable As Word.Table

Set oRange = ActiveDocument.Bookmarks("bmTable").Range
Set oTable = ActiveDocument.Tables.Add(Range:=oRange, _
NumRows:=3, _
NumColumns:=3, _
AutoFitBehavior:=wdAutoFitFixed)

oTable.AutoFormat Format:=wdTableFormatColorful2

Set oRange = Nothing
Set oTable = Nothing
End Sub


I've added an attachment so you can see it in action! :whistle:

asugianto
05-03-2005, 04:04 PM
Hi Joost,

Your macro works great. Thanks a lot. If I want to have the pointer not to move to the first cell in a table; which part of the macro that I have to change?

Thanks,

Alvin

TonyJollans
05-03-2005, 04:47 PM
Very nice, Joost :)

MOS MASTER
05-04-2005, 01:47 PM
Hi Joost,

Your macro works great. Thanks a lot. If I want to have the pointer not to move to the first cell in a table; which part of the macro that I have to change?

Thanks,

Alvin
Hi Alvin,
You're Welcome! :beerchug:

To stop the cursor going to the first cell when you've reached the last cell in the table simply delete the else statement and what goes with it:
Else
Selection.Tables(1).Cell(1, 1).Select

Selection will now stop in the last cell off the table. (While pressing "Tab-Key")

Enjoy! :whistle:


Very nice, Joost :)
Thank you Tony....:hi:

asugianto
05-04-2005, 02:01 PM
Hi Joost,

I figured that one out actually yesterday. Thanks for the answer, though! Great job!

MOS MASTER
05-04-2005, 02:09 PM
Hi Joost,

I figured that one out actually yesterday. Thanks for the answer, though! Great job!
No problem...thats even better! Finding your own sollution means you understand what it does actually!

Till the next round...:whistle:

fumei
05-05-2005, 01:17 PM
Just out of curiosity, why do you use Dim oRange As Word.Range, instead of simply Dim oRange As Range? While true that it is better to have fully explicit declarations (and therefore Word.Range is technically better), at least within Word, "Word" is not needed.

MOS MASTER
05-05-2005, 01:31 PM
Hi Gerry, :D

You've given the answer youreself! :rofl: I always try to use: "fully explicit declarations"

Off course I also understand that it's not neccessary to write out full but I think it improves overall readabillity as well!

I have the same thing with global objects like the Documents collection
I'm always in struggle if I should write Application.Documents.Add instead off Documents.Add

Yes both of them will work and there are many more off this group that should or should not be addressed with there Parent but it's more a thing off personal preference that I try to include the Parent...(But still forget it a lot)

You probably now that this sort off thing get's real important in Office Automation because you will get problems if the proper Parent isn't included in you're code!

:whistle:

fumei
05-05-2005, 01:40 PM
That is what I thought. I think I may get into the habit of that myself. 95% of what I do is in Word...but.....yes, I think that would be a good habit to start.

Thanks Joost. Later, I am outtahere :wavey:

MOS MASTER
05-05-2005, 01:43 PM
That is what I thought. I think I may get into the habit of that myself. 95% of what I do is in Word...but.....yes, I think that would be a good habit to start.

Thanks Joost. Later, I am outtahere :wavey:
Hi Gerry,

Glad you like my habit and I'll make sure to get more consistent!

Thank you to will talk to yah soon...:wavey: