Consulting

Results 1 to 5 of 5

Thread: Creating multiple tables in word with loops (From Excel)

  1. #1
    VBAX Regular Eastwick's Avatar
    Joined
    Mar 2018
    Location
    Newcastle
    Posts
    10
    Location

    Creating multiple tables in word with loops (From Excel)

    Hi,
    I have code that 1) adds and formats tables in word, 2) a second loop then populates the tables using data selected from a multi select listbox. In WORD vba all is good and it works exactly as intended;

    However; now I want to run it from EXCEL VBA and I cannot find a solution for EXCEL (to WORD) VBA to "break" between the Tables. The result is that the first table is overwritten when the second table is created and as the Table increment has by that time advanced to "2", of course "the member does not exist". I have done an extensive amount of searching the past few days and I am afraid it is potentially something very fundamental (I am a newbie at vba).

    Any advice would be greatly appreciated;

    My code is as follows;

    Private Sub CommandButton1_Click()
        'declare variables for word
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
       
        Set objWord = CreateObject("Word.Application")
        'add a new word document
        Set objDoc = objWord.Documents.Add
        objWord.Visible = True
        Dim intIndex As Integer
        Dim intCount As Integer
        Dim tblCount As Integer
        Dim objRange
        
        Set objRange = objDoc.Range
      
        intCount = 0
        'get the number of selected items from the ListBox
    
        With dataForm.ListBox1
            For intIndex = 0 To .ListCount - 1
                If .Selected(intIndex) Then intCount = intCount + 1
            Next
        End With
        'initialise the Table Count
        
        tblCount = 0
       
        Do While intCount > 0
            'declare and add new table
            Dim objTable As Word.Table
            Set objTable = objDoc.Tables.Add(Range:=objRange, NumRows:=6, NumColumns:=3)
            'increment the table count
            tblCount = tblCount + 1
        
            If objDoc.Tables.Count <= 1 Then
          
                With ActiveDocument.Tables(tblCount)
    
                    'A heap of formatting stuff goes in here
                End With
                'More formatting stuff goes in here
                objWord.Selection.Tables(tblCount).Select
                objWord.Selection.Collapse WdCollapseDirection.wdCollapseEnd
                objWord.Selection.InsertAfter vbCr
         
            End If
            intCount = intCount - 1
        Loop
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Private Sub CommandButton1_Click()
        'declare variables for word
        Dim wdApp As New Word.Application
        Dim wdDoc As Word.Document
        Dim wdRng As Word.Range
        Dim wdTbl As Word.Table
        Dim lIndex As Long
        Dim lCount As Long
        
        wdApp.Visible = True
        'add a new word document
        Set wdDoc = wdApp.Documents.Add
        'get the number of selected items from the ListBox
        With dataForm.ListBox1
            For lIndex = 0 To .ListCount - 1
                If .Selected(lIndex) Then lCount = lCount + 1
            Next
        End With
       
        For lIndex = 1 To lCount
            With wdDoc
                Set wdRng = .Range.Characters.Last
                With wdRng
                    .InsertBefore vbCr
                    .Collapse wdCollapseEnd
                    'declare and add new table
                    Set wdTbl = .Tables.Add(Range:=.Duplicate, NumRows:=6, NumColumns:=3)
                    With wdTbl
                        'A heap of table formatting stuff goes in here
                    End With
                    'Document formatting stuff goes in here
                End With
            End With
        Next
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular Eastwick's Avatar
    Joined
    Mar 2018
    Location
    Newcastle
    Posts
    10
    Location
    macropod; many thanks to you. That was the solution - it works as intended and saves a heap of time for my tasks. I still needed a slight mod to create the word object on my system - not sure if that is something to do with MSWord 16 or not. I will post my final code this evening and mark the thread. Again, many thanks to you, Eastwick.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Eastwick View Post
    I still needed a slight mod to create the word object on my system - not sure if that is something to do with MSWord 16 or not.
    Since your original code uses early binding, I'd have thought you'd know you need to set a reference to Word via the VBE's Tools>References. No code changes, per se, are required.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular Eastwick's Avatar
    Joined
    Mar 2018
    Location
    Newcastle
    Posts
    10
    Location
    Ah...Yes! thanks again

Posting Permissions

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