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




Reply With Quote
