Hi,
You must add a reference to your projectVBA
"Microsoft Word xx.x Objects Librairy"
a way of doing :
'---------------------------------------
Sub test()
Dim Rg As Range
Dim Wd As Word.Application
Dim Dc As Document, C As Column
Dim T As Table, P As Row
Dim A As Integer, B As Integer
Dim Bb As Border
'Defined range to copy
With Worksheets("Sheets1")
Set Rg = .Range("A1:D5")
End With
Set Wd = CreateObject("Word.Application")
Wd.Visible = True
Set Dc = Wd.Documents.Add
Set T = Dc.Tables.Add(Range:=Dc.Range, _
NumRows:=Rg.Rows.Count, _
NumColumns:=Rg.Columns.Count)
For A = 1 To Rg.Rows.Count
For B = 1 To Rg.Columns.Count
T.Cell(A, B).Range = Rg(A, B)
Next
Next
'To apply borders if necessary
With T
For Each C In .Range.Columns
C.Borders(wdBorderHorizontal).Visible = True
Next
For Each P In .Range.Rows
P.Borders(wdBorderVertical).Visible = True
Next
For A = -4 To -1
.Range.Borders(A) = True
Next
End With
End Sub
'---------------------------------------
"Fev" a écrit dans le message de groupe de discussion :
...
Hi
I am trying to write some vba code in Excel 2007 to paste a range into
a Word (either 2003 or 2007) table. My code so far, selects the
correct table and selects the top (blank) row in the table. I cannot
get the next part correct. I need to:
1. Insert the correct number of rows in the Word table to accommodate
the Excel data. I have used
noRows = Selection.Rows.Count
2. Select those rows that have been inserted and paste the data to
these rows. When doing this manually it seems to produce the best
results, rather than paste as a nested table etc.
Any suggestions of the correct code would be greeatly appreciated.
Thanks
Heather