PDA

View Full Version : Understanding Ranges in Word 2000 VBA



A.T
02-12-2008, 06:38 AM
Currently, I'm trying to output contents of an Access database into a word document using VBA. I've managed to get the script to output a nice table with the data in, but I'd like some text before the table, describing what is in the table.
If I output the text first and then output the table, the table's range overwrites the original text:



Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add



wrdDoc.Content.InsertAfter "TESTTEXT"
Set objRange = wrdDoc.Range()

wrdDoc.Tables.Add objRange, 1, 3
Set objTable = wrdDoc.Tables(1)

objTable.Cell(1, 1).Range.Font.Bold = True
objTable.Cell(1, 1).Range.Text = "One"
objTable.Cell(1, 2).Range.Font.Bold = True
objTable.Cell(1, 2).Range.Text = "Two"
objTable.Cell(1, 3).Range.Font.Bold = True
objTable.Cell(1, 3).Range.Text = "Three"


This is most likely because the range the table is outputting into is the same as the one the text outputted into, but I'm finding it difficult to find any details on how to control the range properly. Really I'd like to be able to output some text, and then change the range so that that text is then safe from being overwritten.

Thanks.

Tinbendr
02-12-2008, 08:13 AM
After
Set objRange = wrdDoc.Range()
Add

objrange.Collapse wdCollapseEnd

fumei
02-12-2008, 11:11 AM
Tinbendr is correct, and your thought was correct. You put the text in, then made the Range object the entire document - i.e. the text just inserted. You then insert the table using that range (with the text). Collapse the range to its end, then insert the table.

I would also suggest adding a paragraph mark after your text. This will keep the text in its own paragraph.wrdDoc.Content.InsertAfter "TESTTEXT" & vbCrLf

Depending of course on what you are really trying to do, you can make the whole table bold with:
objTable.Range.Font.Bold = True

Also, it may be useful to understand syntax for cells in a table, depending on whether you are using the table object, or the table range.

For a table, you need Row and Column to identify a cell:

objTable.Cells(row, column) - just like you have.

For a table range, you use its index number, i.e. the count, starting from Cell 1 ( that is, table.cell(1, 1)).

objTable.Range.Cells(1)

As an example, say you have a 3 x 3 table:
Dim aTable As Table
Dim var

Set aTable = ActiveDocument.Tables(1)
For var = 1 To aTable.Range.Cells.Count
aTable.Range.Cells(var).Range.Text = _
"Cell number: " & var
Nextwould produce....

A.T
02-12-2008, 01:18 PM
That's great, both of your posts have been very helpful.
Thanks

Is there any detailed documentation out there for the Word VBA classes? Something like ogre3d.org/docs/api/html/classOgre_1_1Bone.html but for VBA?
I've been looking on MSDN, but can only find .NET.

fumei
02-12-2008, 01:44 PM
Office 2000 is no longer supported, and no, you won't find anything on MSDN. However, if you really look, there is lots of stuff on MSDN. Although I admit they do not make it easy.

Word objects
http://msdn2.microsoft.com/en-ca/library/aa220474(office.10).aspx

Word Visual Basic Reference
http://msdn2.microsoft.com/en-ca/library/aa279125(office.10).aspx

Office VBA Reference
http://msdn2.microsoft.com/en-ca/library/aa155054.aspx

These are pointers to 2002. There are some differences (between 2000 and 2002), but not that much. Take a good look at the sidebar menu tree on the left. There are many places to go investigate.

Oh, and Help (F1) is good to use.

fumei
02-12-2008, 01:51 PM
For specifically Range:

http://msdn2.microsoft.com/en-ca/library/aa679615(office.10).aspx

Be sure to check the Working with Range Object link at the bottom.

Even just typing "ranges" into VBA Help will come up with quite a bit of information.

A.T
02-12-2008, 02:58 PM
Brilliant, thanks for those.

Looks like I've got some reading to do :)

fumei
02-13-2008, 11:46 AM
Yup. However, I will state strongly that if you really really understand how to use Range in Word, you will be way ahead. It is one of the most crucial concepts in the Word Object Model.