Re: need help with table code




"Michael F" <MichaelF@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:E3309732-22CD-424E-84CA-D01F9E7C5534@xxxxxxxxxxxxxxxx
Jonathan,

Thanks for the reworked code. I tried it one statement at a time to
understand what each statement did. I need a recommendation for a good book
on VBA for Word.

Book reviews here

Book Recommendations
http://www.word.mvps.org/Tutorials/BookRecommendations.htm


How is it that the Set oTable... statement actually creates a table rather
than just storing it in oTable?

This line creates a table in the document

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=txtRows, _
NumColumns:=txtColumns

This line creates the same table in the document, and also says, in effect "for the prupsoes of this macro, this table is called oTable. When you want to refer to anything in this table, you can use this name."

If you know that the table is the first table in the document, then oTable is exactly the same as ActiveDocument.Range.Tables(1). The advantage of usng oTable this way is that you now have a reference to the table, wherever it is, and it will continue to point to that specific table even if you insert other tables elsewhere in the document.


I could not find any online help that
explained what Dim oTable as Table meant other than creating a variable.

Yes, it is creating an object variable. The specific object is a Table obejct.


In the next statement, it looks like oTable is an object and you are setting
the style for the range that is the table. Did I get that right?
oTable.Range.Style = "Table cell"

Pretty close. The oTable object has a Range property, which in turn is a Range object which has a Style property, and we are assigning a new name for that style.


In the next statement that applies a style to the first row, I could not
figure out how you came up with it.
oTable.Range.Rows.First.Range.Style = "Table heading"

read backwards again. We are

- giving the name "Table heading"
- to the Style
- of the Range
- of the First
- Row
- of the range
- of the table pointed to by the oTable variable

(Actually, one of those ranges is superfluous, the Table objact has a Rows property directly, we don't need a Range in there. So that line could be as follows
oTable.Rows.First.Range.Style = "Table heading")


I tried using the VBA trick of entering a string then a period and letting
VBA prompt with a list of valid terms, but I could not quite figure out how
you figured out the path.

Lots of practice :-)


I have two other settings I would like to make:
- setting the table indent

oTable.Rows.LeftIndent = CentimetersToPoints(1.5)

- setting the table width

'set a fixed width
oTable.PreferredWidthType = wdPreferredWidthPoints

'decide what the width should be
oTable.PreferredWidth = CentimetersToPoints(16)


I was able to figure out how to set the indent with
oTable.Rows.LeftIndent = InchesToPoints(0.1)
but I tried oTable.PreferredWidth = "4.75" for the table width and it jammed
all the columns into the smallest width (less than an inch).

Almost every dimension is specified in points in Word VBA, and a table that is only 4.75pt wide is pretty narrow!

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

.



Relevant Pages

  • Read headings from CSV into Form in VBA?
    ... Im using VBA to develop ArcGIS so that I can add added functionality ... They want to have 4 top heading categories which you can only ... they could have a simple Excel csv file - ... drop-down - Ive sourced the drop-down items to a text file for now ...
    (microsoft.public.office.developer.vba)
  • Re: Retrieving Previous Level Number from heading style
    ... from a nominated heading style whether that heading level ... but a Yes or No to include numbering from a previous level. ... The Microsoft advice on equivalent Wordbasic - VBA ...
    (microsoft.public.word.vba.general)
  • Re: Use current heading level in a field
    ... Dim CurrStyle As Style ... but without vba ...... ... Outline Level of the immediately preceding heading. ...
    (microsoft.public.word.vba.general)
  • Re: Looking for recommendation on C# Book
    ... I don't have a book recommendation for you. ... If you've never programmed in a language other than VBA, ...
    (comp.databases.ms-access)
  • Re: Mixed Font Format in one Style
    ... at the reference more closely, as this was what I wanted, and realized I ... I defined my own "Heading" as a hanging indent and inserted a tab at the ... When I hide the Heading para mark, poof I get exactly what I want! ... > does make your life more simple--looking for an alternative to a VBA ...
    (microsoft.public.word.pagelayout)