View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
macropod[_2_] macropod[_2_] is offline
external usenet poster
 
Posts: 293
Default Copy to a Word Table

Hi Heather,

Here's a quick demo of how you can populate a table with data. One thing your draft code is missing is the column count - you'll
need that.

Sub TableDemo()
Dim IntRows, IntCols As Integer
Dim i, j As Integer
IntRows = 5
IntCols = 6
With ActiveDocument
.Tables.Add Range:=.Bookmarks("CommTable").Range, NumRows:=IntRows, NumColumns:=IntCols
With .Bookmarks("CommTable").Range.Tables(1)
For i = 1 To IntRows
For j = 1 To IntCols
.Cell(i, j).Range.Text = "Row: " & i & ", Column: " & j
Next
Next
End With
End With
End Sub

For what you're trying to do, I don't believe you need to copy the Excel data; just replicate it. In the above demo, I've supplied
both the row count and the column count and I've used these to populate the cells in place of the Excel data (this is, after all,
just a demo). Note that there is no need to select anything - simply point the code to the 'CommTable' bookmark's range.

Of course, if you're going to use a copy/paste operation, you could create the table (after copying the Excel data) via the
PasteSpecial method in Word:

Sub PasteDemo()
ActiveDocument.Bookmarks("CommTable").Range.PasteS pecial Link:=False, _
DataType:=wdPasteRTF, Placement:=wdInLine, DisplayAsIcon:=False
End Sub


--
Cheers
macropod
[Microsoft MVP - Word]


"Fev" wrote in message ...
On Jan 20, 8:45 am, joel wrote:
I'm not an expert in Word VBA so I think part of the answer you need to
get from the word expert. I agree with the method you are using. Make
sure you copy and then use PasteSpecial into word using the correct word
property values in pastespecial. to get the property value number go to
Word VBA help or add the reference to the excel VBA by using the menu
option in VBA Tools : Refernce : Microsoft Word XX.0 Object Library.

You can get the Word property names by going to object browser (VBA
menu View Object Browser) and typing into the binoculars box
pastespecial. When adding the Refernce to word the list of values are
different. Irecently help somebody do something similar to Power Point
and using the standard excel values gave errors in 2007 and didn't give
errors in 2003.

In word select the exact size of the range youi are copying from excel.
In excel you only have to select the first location, in word it is
better to select the entire range. I have had problems pasting into
word if I only select the first cell. sometimes word puts all the excel
rows into one row of the word table.

Word is very tricky in VBA to select different areas. I've tried a lot
of times and spend hours doing something I can do in excel in a couple
of seconds.

--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=171622

Microsoft Office Help


Hi I can do the programming in Word in VBA, that is quite easy, the
problem is that I need to write the code in Excel VBA to push the data
through to the Word table. Hence my positng on the Excel VBA forum.
The Word code would be along the lines of:
Selection.GoTo What:=wdGoToBookmark, Name:="CommTable"
Selection.InsertRowsAbove noRows
Selection.MoveDown Unit:=wdLine, Count:=noRows, Extend:=wdExtend
Selection.Paste
But to address the word table from Excel is where I'm stuck.
My code so far in Excel is:

Sub WordTable()
Dim i As Integer
Dim appWd As Word.Application
Dim mydoc As Document

ActiveDocument.Tables(2).Rows(1).Select

Thanks for the suggestions, but I still don't get what I need to
replace Selection. (I think that is the problem) with in Excel to get
this to work.
Thanks
Heather