PDA

View Full Version : [SOLVED:] Creating multiple tables in word with loops (From Excel)



Eastwick
08-07-2018, 01:05 AM
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; :banghead:

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

macropod
08-07-2018, 03:29 AM
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

Eastwick
08-07-2018, 02:51 PM
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.

macropod
08-07-2018, 03:09 PM
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.

Eastwick
08-07-2018, 06:55 PM
Ah...Yes! thanks again :yes