Re: Multiple Tables and Ranges by Dave
Dave
Fri Aug 06 12:10:55 CDT 2004
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
"scorpion53061" <scorpion_53061@nospamhereeveryahoo.com> wrote in message
news:%235M76O8eEHA.1724@TK2MSFTNGP10.phx.gbl...
>
> "Dave Lett" <dlett@NOsmconst.comSPAM.com> wrote in message
> news:uYrUPI8eEHA.2604@TK2MSFTNGP12.phx.gbl...
> > 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
>
>