Multiple Tables and Ranges

S

scorpion53061

hi I would like to see vba code code tutorial online to have 3 tables with 3
different ranges so I can address teh cell of each table in each range. I
run the macros of course but they do not seem to use this method.

Thank you for your help.
 
D

Dave Lett

Hi scorpion53061,

It sounds like you might be having a problem getting the text from a cell
without the end of cell character. Maybe the following code helps with your
task:

Dim oTbl As Table
Dim oCl As Cell
Dim oRngCl As Range
For Each oTbl In ActiveDocument.Tables
For Each oCl In oTbl.Range.Cells
Set oRngCl = oCl.Range
oRngCl.MoveEnd Unit:=wdCharacter, Count:=-1
MsgBox oRngCl.Text
Next oCl
Next oTbl

HTH,
Dave
 
S

scorpion53061

Hi Dave,

Thanks for responding.

My problem is that in my header I have three tables in which I am trying to
address specific cells with specific text.

I tried the methods used in teh macros... such as move down here, move up
there and it works if you step through it but if you run it on its own for
whatever reason it does not. This is an example of what I had :

oDoc.Content.Application.Selection.TypeText(Text:="Order Report")
oDoc.Content.Application.Selection.MoveDown(Unit:=wdLine,
Count:=1)
oDoc.Content.Application.Selection.MoveLeft(Unit:=wdCharacter,
Count:=1)

Often it did not do what was expected.
Someone suggested using a "range" method to get text into cells in a post I
saw yesterday. I am okay with one table. But how do I am having problem with
additional tables.

Again thank you....

I am (e-mail address removed) (remove "nospam") on MSN Messenger if you have it
so feel free to drop a line if you have any thoughts or respond here is fine
too.
 
D

Dave Lett

Hi scorpion53061,

Okay, now we're getting somewhere.
You have a total of 3 tables in your header, correct?
In table 1, what cell do you want to modify? How do you want to modify it?
In table 2, what cell do you want to modify? How do you want to modify it?
In table 3, what cell do you want to modify? How do you want to modify it?
Can you bookmark the text in these cells (if so, this might make the code a
lot easier for you to read/write)?

Dave
 
S

scorpion53061

Dave Lett said:
Hi scorpion53061,

Okay, now we're getting somewhere.
You have a total of 3 tables in your header, correct?
In table 1, what cell do you want to modify? How do you want to modify it?
In table 2, what cell do you want to modify? How do you want to modify it?
In table 3, what cell do you want to modify? How do you want to modify it?
Can you bookmark the text in these cells (if so, this might make the code a
lot easier for you to read/write)?

Table 1 has 25 cells (13 rows) with 2 columns each needing their own text. I
want to address cell alignemnt, text, font and in one case color.

In one row the cells get merged.

Table 2 has 6 cells with 2 rows and 3 columns.Again the same I want to
modify. I get in trouble here when I name it Table(2) because it says the
"member of the collection does not exist"

I am unsure on bookmarking. I am pretty understanding of how the cell
numbering is working I think. But I will show what i have so far. I dont
have table 2 here yet.

Again thank you.



Dim myrange As Word.Range

myrange = oDoc.Content.Application.Selection.Range
oDoc.Content.Application.Selection.Tables.Add(Range:=myrange,
NumRows:=6, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _
wdAutoFitFixed)

oDoc.Content.Application.Selection.Tables.Item(1).Cell(3, 1).Range.Select()
With oDoc.Content.Application.Selection.Font
.Name = "Arial"
.Size = 18
.Bold = False
.Italic = False
End With
With oDoc.Content.Application.Selection.Tables.Item(1).Cell(3,
1)
.Range.Text = "A Division of Superior Electrical Supply
Inc."
.Row.Alignment = 0
.VerticalAlignment = 3
End With
oDoc.Content.Application.Selection.Tables.Item(1).Cell(3,
1).Range.Text = "JJ Koepsell Company"
oDoc.Content.Application.Selection.Tables.Item(1).Cell(3,
2).Range.Select()
With oDoc.Content.Application.Selection.Font
.Name = "Arial Black"
.Size = 28
.Bold = False
.Italic = False
.Color = 3355443 'wdColorGray80
End With
oDoc.Content.Application.Selection.Tables.Item(1).Cell(3,
2).Range.Text = "Order Report"
oDoc.Content.Application.Selection.Tables.Item(1).Cell(4,
1).Range.Select()
With oDoc.Content.Application.Selection.Font
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With oDoc.Content.Application.Selection.Tables.Item(1).Cell(4,
1)
.Range.Text = "A Division of Superior Electrical Supply
Inc."
.Row.Alignment = 0
.VerticalAlignment = 0
End With

oDoc.Content.Application.Selection.Tables.Item(1).Cell(6,
1).Range.Select()
With oDoc.Content.Application.Selection.Font
.Name = "Arial"
.Size = 10
.Bold = False
.Italic = False
End With
With oDoc.Content.Application.Selection.Tables.Item(1).Cell(6,
1)
.Range.Text = "1010 S. Ninth Street"
End With
 
D

Dave Lett

Hi

Let's start slowly because there are a handful of things that you can do to
improve your routine. First, I think you're overcomplicating things by
taking a very long approach to your objects. For example, have at look at
the following:
this is your original
oDoc.Content.Application.Selection.Tables.Item(1).Cell(3, 1).Range.Select
which does the same this as this (and this is a lot easier to read)
oDoc.Tables(1).Cell(3, 1).Select

The following, too:
myrange = oDoc.Content.Application.Selection.Range
myrange = oDoc.Selection.Range

However, in terms of good programming practice, it's better to use the range
object instead of the selection object. The most notable reasons are speed
and the fact that the range object doesn't require you to select anything
and, therefore, your user's screen won't jump (the changes will just
"magically" appear). So, let's go through an example:
We're not going to select anything, but we will change the text/formatting
of one your cells (for this sample, use a blank document).

Dim oTbl As Table
Dim oCl As Cell
Dim oRng As Range
Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=6, _
NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
Set oRng = oTbl.Cell(3, 1).Range
Set oCl = oTbl.Cell(3, 1)
With oCl
.Row.Alignment = 0
.VerticalAlignment = 3
With .Range
.Text = "Enter text here"
With .Font
.Name = "Arial"
.Size = 18
.Bold = False
.Italic = False
End With
End With
End With

''' if you want to change the text of a different cell
''' then simply redefine oCl, as in the following:
Set oCl = oTbl.Cell(6, 2)
With oCl
.Row.Alignment = 0
.VerticalAlignment = 3
With .Range
.Text = "Enter text here"
With .Font
.Name = "Arial"
.Size = 18
.Bold = False
.Italic = False
End With
End With
End With

''' if you want to add a new table, then simply use the same
''' construction as above, but make sure that you're not
''' at the end of the previous table or Word will simply
''' appeand the next table to the previous table (making
''' one, long table
Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=6, _
NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)

HTH,
Dave
 
S

scorpion53061

You have been very helpful. Thank you.


Dave Lett said:
Hi

Let's start slowly because there are a handful of things that you can do to
improve your routine. First, I think you're overcomplicating things by
taking a very long approach to your objects. For example, have at look at
the following:
this is your original
oDoc.Content.Application.Selection.Tables.Item(1).Cell(3, 1).Range.Select
which does the same this as this (and this is a lot easier to read)
oDoc.Tables(1).Cell(3, 1).Select

The following, too:
myrange = oDoc.Content.Application.Selection.Range
myrange = oDoc.Selection.Range

However, in terms of good programming practice, it's better to use the range
object instead of the selection object. The most notable reasons are speed
and the fact that the range object doesn't require you to select anything
and, therefore, your user's screen won't jump (the changes will just
"magically" appear). So, let's go through an example:
We're not going to select anything, but we will change the text/formatting
of one your cells (for this sample, use a blank document).

Dim oTbl As Table
Dim oCl As Cell
Dim oRng As Range
Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=6, _
NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
Set oRng = oTbl.Cell(3, 1).Range
Set oCl = oTbl.Cell(3, 1)
With oCl
.Row.Alignment = 0
.VerticalAlignment = 3
With .Range
.Text = "Enter text here"
With .Font
.Name = "Arial"
.Size = 18
.Bold = False
.Italic = False
End With
End With
End With

''' if you want to change the text of a different cell
''' then simply redefine oCl, as in the following:
Set oCl = oTbl.Cell(6, 2)
With oCl
.Row.Alignment = 0
.VerticalAlignment = 3
With .Range
.Text = "Enter text here"
With .Font
.Name = "Arial"
.Size = 18
.Bold = False
.Italic = False
End With
End With
End With

''' if you want to add a new table, then simply use the same
''' construction as above, but make sure that you're not
''' at the end of the previous table or Word will simply
''' appeand the next table to the previous table (making
''' one, long table
Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=6, _
NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)

HTH,
Dave


text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top