Thanks!
I have a program that manage excel tables and put the data into word. I
adjust the format into word depending of the excel format. To do that, I
merge the cells into Excel as it will be in Word. So the number of cells
inside a row is always the same. The program works very slow when I create
the table into word. I tried to hide the word application but the table
formatted is not the same?? it's very strange.
Thank you!
Alex
appWord.Selection.PasteExcelTable LinkedToExcel:=False,
WordFormatting:=False, RTF:=False
Set tbl = docWord2.Tables(iTabNb)
docWord2.Bookmarks(1).Delete
End If
tbl.Select 'Set standard table format
appWord.Activate 'voir si toujours erreur...
With appWord.Selection.ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
End With
'3- Adjust column width
With tbl.Rows
.LeftIndent = 0
End With
' With ActiveDocument.PageSetup
' UsableWidth = .PageWidth - .LeftMargin - .RightMargin
' End With
UsableWidth = 432 '6 inches
TableWidth = 0
iCol = oXlRng.Columns.Count 'Expect no merge cell on the first line

UsableWidth = UsableWidth - iCol * 5.6 'each column have a 5.6 width
margin
For i = 1 To iCol
TableWidth = TableWidth + oXlRng.Columns(i).Width
Next i
' MsgBox ("iTab:" & iTab & " TableWidth:" & TableWidth & "
UsableWidth:" & UsableWidth)
'Verify potential problems
If TableWidth > UsableWidth Then MsgBox ("Table Width > Word Space for
table #" & iTab)
'Enlarge table to fit 100% page
For i = 1 To iCol
ColWidth(i) = 5.6 + oXlRng.Columns(i).Width '* (UsableWidth /
TableWidth)
Next i
For i = 1 To oXlRng.Rows.Count
c = 1
j = 1
tbl.Cell(i, 1).Select
appWord.Selection.SelectRow
iCell(i) = appWord.Selection.Cells.Count
'iCell(i) = tbl.Rows(i).Cells.Count 'POUR EXCEL
Do Until c > iCell(i)
CellWidth(i, j) = 0
k = oXlRng.Cells(i, j).MergeArea.Count 'nombre de fusion
For n = 1 To k
CellWidth(i, j) = CellWidth(i, j) + ColWidth(j + n - 1)
Next n
r(i, c) = j
c = c + 1
j = j + k
Loop
Next i
For i = 1 To tbl.Rows.Count
For j = 1 To iCell(i)
Set pCell = tbl.Cell(i, j)
pCell.Width = CellWidth(i, r(i, j))
Next j
' tbl.Rows(i).HeightRule = wdRowHeightAtLeast
' tbl.Rows(i).Height = CentimetersToPoints(0.56 * oXlRng.Rows(i).Height /
12.75)
Next i
For i = 1 To tbl.Rows.Count
Next i
'5- Add borders
With tbl
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
For i = 1 To tbl.Rows.Count
For j = 1 To iCell(i)
With oXlRng.Cells(i, r(i, j))
With .Borders(xlEdgeTop)
If .LineStyle = xlContinuous Then
tbl.Cell(i, j).Borders(wdBorderTop).LineStyle =
wdLineStyleSingle
If .Weight = xlThin Then
tbl.Cell(i, j).Borders(wdBorderTop).LineWidth =
wdLineWidth025pt
ElseIf .Weight = xlMedium Then 'Or .Weight = xlLarge Then
tbl.Cell(i, j).Borders(wdBorderTop).LineWidth =
wdLineWidth150pt
End If
Else
tbl.Cell(i, j).Borders(wdBorderTop).LineStyle =
wdLineStyleNone
End If
End With
With .Borders(xlEdgeBottom)
If .LineStyle = xlContinuous Then
tbl.Cell(i, j).Borders(wdBorderBottom).LineStyle =
wdLineStyleSingle
If .Weight = xlThin Then
tbl.Cell(i, j).Borders(wdBorderBottom).LineWidth =
wdLineWidth025pt
ElseIf .Weight = xlMedium Then
tbl.Cell(i, j).Borders(wdBorderBottom).LineWidth =
wdLineWidth150pt
End If
Else
tbl.Cell(i, j).Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
End If
End With
With .Borders(xlEdgeLeft)
If .LineStyle = xlContinuous Then
tbl.Cell(i, j).Borders(wdBorderLeft).LineStyle =
wdLineStyleSingle
If .Weight = xlThin Then
tbl.Cell(i, j).Borders(wdBorderLeft).LineWidth =
wdLineWidth025pt
ElseIf .Weight = xlMedium Then
tbl.Cell(i, j).Borders(wdBorderLeft).LineWidth =
wdLineWidth150pt
End If
Else
tbl.Cell(i, j).Borders(wdBorderLeft).LineStyle =
wdLineStyleNone
End If
End With
With .Borders(xlEdgeRight)
If .LineStyle = xlContinuous Then
tbl.Cell(i, j).Borders(wdBorderRight).LineStyle =
wdLineStyleSingle
If .Weight = xlThin Then
tbl.Cell(i, j).Borders(wdBorderRight).LineWidth =
wdLineWidth025pt
ElseIf .Weight = xlMedium Then
tbl.Cell(i, j).Borders(wdBorderRight).LineWidth =
wdLineWidth150pt
End If
Else
tbl.Cell(i, j).Borders(wdBorderRight).LineStyle =
wdLineStyleNone
End If
End With
End With
sData = oXlRng.Cells(i, r(i, j))
If IsNumeric(sData) And sData <> "" Then
If sData <> 0 Then 'on veut pas formater les "-"
sFormat = oXlRng.Cells(i, r(i, j)).NumberFormat
If Left(sFormat, 15) = "#,##0_);(#,##0)" Or Left(sFormat, 15) =
"# ##0_-;(# ##0)" Or Left(sFormat, 15) = "#,##0_-;(#,##0)" Then
sFormat = "#,##0;(#,##0)"
End If
If sFormat <> "General" Then sData = Format(sData, sFormat)
If Right(sData, 1) = ")" Then
.Cell(i, j).Select
With appWord.Selection.ParagraphFormat
.RightIndent = CentimetersToPoints(-0.12)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
Else
.Cell(i, j).Select
With appWord.Selection.ParagraphFormat
.RightIndent = CentimetersToPoints(0)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
End If ')"
.Cell(i, j).Range.Text = sData 'Transformation seulement
pour les nombres
ElseIf sData <> "" Then 'cause prob de mettre cette ligne avec
ligne suivante
' If Right(sData, 1) = "%" And IsNumeric(Left(sData, Len(sData) -
1)) And Len(sData) > 1 Then
' sData = Left(sData, Len(sData) - 1) & " %"
' .Cell(i, j).Range.Text = sData 'Transformation seulement pour
les nombres
' End If
End If
End If 'IsNumeric
Next j
Next i
End With