Question for Jay Freedman

E

Edward

Hi Jay,
I used your code to insert two types of table but i have a problem
separating one of them
Sub InsertTable()
Dim myRg As Range
Dim myTbl As Table
Dim Tnum As Integer
Dim PName As String
Dim CurrName As String
Dim flagNew As Boolean
Dim count As Long
Dim RN As Long
Dim countflag As Long
countflag = 0
ne = 3
RN = 2
count = 2
PName = ""
'populate excel array
Call createArrayfromExcel
For Tnum = 1 To numofRows

CurrName = excelArray(Tnum, 1)

If CurrName <> PName Then
ne = 3
PName = CurrName
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd
*************************************
myRg.Text = vbCr <---------------- this code is not separating the
table
****************************
Set myTbl = ActiveDocument.Tables.Add( _
Range:=myRg, NumRows:=1, NumColumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)

With myTbl
.Borders(wdBorderBottom).LineStyle = wdLineStyleDouble
.Borders(wdBorderTop).LineStyle = wdLineStyleDouble
.Borders(wdBorderLeft).LineStyle = wdLineStyleDouble
.Borders(wdBorderRight).LineStyle = wdLineStyleDouble

.Rows(1).Height = 50
.Columns(1).Width = 390
.Columns(2).Width = 140
.AutoFitBehavior (wdAutoFitWindow)
.Cell(1, 1).Range.Text = excelArray(Tnum, 1) & vbCr &
excelArray(Tnum, 2) & vbCr & excelArray(Tnum, 3) & vbCr & excelArray(Tnum, 4)
.Cell(1, 1).Range.Paragraphs(1).Style =
ActiveDocument.Styles(wdStyleHeading2)
.Cell(1, 1).Range.Font.Size = 11
.Cell(1, 2).Range.Text = vbCr & "Phone:" & excelArray(Tnum, 5) &
vbCr & vbCr & "Fax:" & excelArray(Tnum, 6)
.Cell(1, 2).Range.Font.Size = 11

End With

' add a paragraph to separate the tables
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd
myRg.Text = vbCr


num = Tnum
flagNew = False
Set myRg = ActiveDocument.Range
myRg.Collapse wdCollapseEnd
Set myTbl = ActiveDocument.Tables.Add( _
Range:=myRg, NumRows:=2, NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)


With myTbl

.Rows(1).Shading.BackgroundPatternColor = wdColorGray25
.Rows(1).Range.Font.Size = 11
.Cell(1, 1).Range.Text = "Contact:"
.Cell(1, 1).Range.Font.Bold = True
.Cell(1, 2).Range.Text = "Office:"
.Cell(1, 2).Range.Font.Bold = True
.Cell(1, 3).Range.Text = "Mobile:"
.Cell(1, 3).Range.Font.Bold = True

.Rows(2).Range.Font.Size = 11
.Cell(2, 1).Range.Text = excelArray(num, 7)
.Cell(2, 2).Range.Text = "Phone:" & excelArray(num, 8) & vbCr &
"Fax:" & excelArray(num, 9) & vbCr & "E-Mail:" & excelArray(num, 10)
.Cell(2, 3).Range.Text = "Mobile:" & vbCr & excelArray(num, 11)
.Columns(1).Width = 176
.Columns(2).Width = 235

.AutoFitBehavior (wdAutoFitWindow)

' etc. for rest of header row

' .Rows.Add
' Wend or Next

' after last row of data is added,
' delete the empty row that isn't needed
' .Rows.Last.Delete
End With
Else
myTbl.Rows.Add
With myTbl
.Rows(ne).Range.Font.Size = 11
.Cell(ne, 1).Range.Text = excelArray(num, 7)
.Cell(ne, 2).Range.Text = "Phone:" & excelArray(num, 8) & vbCr &
"Fax:" & excelArray(num, 9) & vbCr & "E-Mail:" & excelArray(num, 10)
.Cell(ne, 3).Range.Text = "Mobile:" & vbCr & excelArray(num, 11)
End With
ne = ne + 1
End If
Next Tnum
End Sub


I have marked part of the code which doesn't separate the table and I can't
figure out why. I have two type of table one has 2 columns and one has three
columns and they alternate in my document the table which has two columns
always has the same format ( i row and two columns) but the tbale which has
three columns has variable number of rows based on the number of records i
import from excel , this code can separat the first table but can't separate
the second table from following table
 
E

Edward

Nevermind I had to add one additional myRg.Collapse wdCollapseEnd and it
works now
Thanks
 
J

Jay Freedman

I'm always happy to see people solve their own problems. :) That .Collapse is
exactly what I would have suggested.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top