Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Creating tables in loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tborthwick@gmail.com - 10 Mar 2008 23:04 GMT
Hello,

I'm trying to loop through some information, creating some text and a
table at each pass. What I have now (see below) places each table into
the first cell of the preceding table. It probably is an error in my
range but I'm not sure what it should look like. I thought using
Collapse would position the range at the current insertion point but
it doesn't seem to. Any help would be appreciated.

Thanks,

Tom

Sub Main
    Dim Word As Object
    Dim Docs As Object
    Dim WordBasic As Object
    Dim ActiveDoc As Object

    Dim diag As Diagram
    Dim mdl As Model
    Dim sm As SubModel
    Dim so As SelectedObject
    Dim id As Integer
    Dim ent As Entity
    Dim attr As AttributeObj

    Set Word = CreateObject("Word.Application")
    Word.Visible = True
    Word.Options.CheckGrammarAsYouType = False
    Word.Options.CheckSpellingAsYouType = False

    Set ActiveDoc = Word.Documents.Add()

    Set diag = DiagramManager.ActiveDiagram
    Set mdl = diag.ActiveModel
    Set sm = mdl.ActiveSubModel

   For Each so In sm.SelectedObjects
        If so.Type = 1 Then
            id = so.ID
            Set ent = mdl.Entities.Item(id)

            Word.Selection.TypeText Text:=ent.EntityName & vbCrLf
            Word.Selection.TypeText Text:=ent.Note & vbCrLf

            Set objRange = Word.Selection.Range
            objRange.Collapse Direction:=0

            Set objTable = objRange.Tables.Add(Range:=objRange,
NumRows:=ent.Attributes.Count, NumColumns:=3)

            Dim curRow As Integer
            curRow = 1

            For Each attr In ent.Attributes
                objTable.Cell(curRow, 1).Range.Text = attr.ColumnName
                objTable.Cell(curRow, 2).Range.Text = attr.Datatype
                objTable.Cell(curRow, 3).Range.Text = attr.Notes

                curRow = curRow + 1
            Next
            objRange.Collapse Direction:=0
            objRange.Select()

        End If
   Next
End Sub
Doug Robbins - Word MVP - 11 Mar 2008 08:19 GMT
Declare a Range object

Dim myrnge as Range

Then

For Each so In sm.SelectedObjects
   If so.Type = 1 Then
       id = so.ID
       Set ent = mdl.Entities.Item(id)
       Set myrnge = ActiveDoc.Range
       myrange.Collapse wdCollapseEnd
       myrange.InsertAfter ent.EntityName & vbCrLf & ent.Note & vbCrLf
       Set myrnge = ActiveDoc.Range
       myrnge.Collapse wdCollapseEnd
       Set objTable = objRange.Tables.Add(Range:=myrnge, _
        NumRows:=ent.Attributes.Count, NumColumns:=3)

       'etc

   End If
Next

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hello,
>
[quoted text clipped - 64 lines]
>    Next
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.