Consulting

Results 1 to 8 of 8

Thread: Understanding Ranges in Word 2000 VBA

  1. #1

    Understanding Ranges in Word 2000 VBA

    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.

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    After
    [vba] Set objRange = wrdDoc.Range()[/vba]
    Add
    [vba]
    objrange.Collapse wdCollapseEnd
    [/vba]

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.[vba]wrdDoc.Content.InsertAfter "TESTTEXT" & vbCrLf[/vba]

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

    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:
    [vba]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
    Next[/vba]would produce....

  4. #4
    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.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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/lib...ffice.10).aspx

    Word Visual Basic Reference
    http://msdn2.microsoft.com/en-ca/lib...ffice.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.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    For specifically Range:

    http://msdn2.microsoft.com/en-ca/lib...ffice.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.

  7. #7
    Brilliant, thanks for those.

    Looks like I've got some reading to do

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •