Table with macro

R

RBlunden

I have a table of labels I want to print, but I want to add a piece of text
to each cell. What macro commands do I use to say 'in each cell paste from
clipboard'?
Is there an array or do I select the whole table and use a 'for each item'?
Sorry, but my word macro writing is rather rusty.
Many thanks,
Rich.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UkJsdW5kZW4=?=,
I have a table of labels I want to print, but I want to add a piece of text
to each cell. What macro commands do I use to say 'in each cell paste from
clipboard'?
Is there an array or do I select the whole table and use a 'for each item'?
How "efficiently" this can be accomplished depends to a certain extent on the
version of Word you have. There is no built-in way to pick up the table in an
array. Although it would be possible, you might run into more problems than
it's worth if you have to maintain formatting or special characters.

The simplest thing to program would be to "walk" each cell in the table.
Assuming what you want to paste should go at the end of each cell, in a new
line, something like this:

Dim tbl as Word.Table
Dim cel as Word.Cell
Dim rng as Word.Range

Set tbl = ActiveDocument.Tables(1)
For Each cel in tbl.Range.Cells
Set rng = cel.Range
rng.Collapse wdCollapseEnd
rng.Paste
Next

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

Greg

Cindy,

I tried your code with interesting results. Your code as written
pastes the clipboard contents to the start of the second and each
subequent cell to the end of the table and place the final paste at the
end of the table.

I next tried this:

Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
End Sub

This too had interesting results. Particularily if I select and copy a
complete word or phrase (without the leading space). The result is the
text is pasted to all cells as expected, but a run time error is
generated.

Run Time Error '5825' Object has been deleted. :-(

If I copy only part of a word or a word/phase including a leading
space, all is well.

I have discovered that the cause of this behaviour is
Tools>Options>Edit>Use smart cut and paste. With that option off, the
procedure works as expected with no run time error. That is the cause,
but I don't really understand the why.

I suppose that knowing this you could use an error handler:

Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
On Error GoTo Handler
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
Exit Sub
Handler:
End Sub
 
G

Greg

or maybe use the VBA to get around the smart paste issue:

Sub Test3()
Dim MyData As DataObject
Dim myString As String
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range

Set MyData = New DataObject
MyData.GetFromClipboard
myString = MyData.GetText
Set tbl = ActiveDocument.Tables(1)
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.InsertAfter myString
Next
End Sub
 
G

Greg Maxey

Cindy,

These results where seen using Word2000. Testing now with Word2003, there
is no run time error.
 
R

RBlunden

Thanmks all,
I tried Cindy's code in 2003 and found it left the first cell empty but
added the clipboard data outside the table at the end.
Greg, I tried yours and it worked fine.
Thanks all for your help. Will use this code for sure.

Rich
 

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