Odd vbTab behaviour when posting to a table in MS Word

RobMatthews

Board Regular
Joined
Nov 16, 2008
Messages
81
I'm copying some data from various places on spreadsheet into a text string on the clipboard, and wanting to manually paste into both a table in Word, and a table in an Outlook email.

The problem is that the spcl character vbTab does not seem to be recognised by either Word or Outlook in the context of a table. The only exception is if more than one cell in the target table is selected.

To test this, Run this code in Excel (or in word, I guess), then open Word and create a table, put the cursor into a cell in the table, and hit Ctrl-V.

Then highlight more than one cell in the table, and Ctrl-V again.

Code:
Private Sub TestCopyWithTab()Dim sString As String
Dim DataObj As New MSForms.DataObject
sString = "FirstCell" & vbTab & "2nd Cell"
DataObj.SetText sString
DataObj.PutInClipBoard
End Sub

I have also tried Chr(9) and a couple of other options as replacements for vbTab, but I can't figure it out.

Please help.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
To write to table cells with code, you need to address each cell individually; unless you resort to SendKeys (not recommended), you can't use vbTab to move between cells.
 
Upvote 0
Thanks for your reply.

I wonder why that is; something to do with the way the tables work, I suppose. I was hoping to just do a dumb paste into Word and Outlook rather than do it programmatically.

Oh well.

Cheers.
 
Upvote 0
Well, if you're copying from Excel, you could select the appropriate location in the destination table in Word, then use:
Selection.PasteAppendTable
PasteAppendTable merges pasted cells into an existing table by inserting the pasted rows between the selected rows. No cells are overwritten.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top