Convert Table to OLE Word Document

K

kbmurphy

Hello all,

I've got a VBA function that attempts to take a word table, select it,
cut it, and then create a Word OLE object where the table used to be,
then pastes the table into the Word OLE. I need this for a program I
work with that doesn't play nice with Word tables.

The code is below, and it works in Word 2003, but it will not work for
all tables in a larger document. I'm not sure why, and was wondering
if anyone had any ideas.

Thanks in advance!

Sub ConvertTablesToOLE()
'


'Move to top of document

Selection.GoTo What:=wdGoToPage, which:=wdGoToFirst


'Get count of tables
Dim tCount As Integer
Dim i As Integer
tCount = ActiveDocument.Tables.Count

i = 1

While i <= tCount


Selection.GoTo What:=wdGoToTable, which:=wdGoToNext 'go to first cell
of next table
Selection.MoveUp Count:=1
Selection.EndKey
Selection.TypeParagraph
Selection.MoveDown Count:=1
Selection.Tables(1).Range.Select
Selection.Cut
Selection.MoveUp Count:=1


'Create OLE, paste into it

Selection.InlineShapes.AddOLEObject ClassType:="Word.Document.8",
FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False
ActiveWindow.Selection.Paste
ActiveWindow.Close



i = i + 1
tCount = ActiveDocument.Tables.Count 'We now have one less table, so
tCount gets updated

Wend

End Sub
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hello all,

I've got a VBA function that attempts to take a word table, select it,
cut it, and then create a Word OLE object where the table used to be,
then pastes the table into the Word OLE. I need this for a program I
work with that doesn't play nice with Word tables.

The code is below, and it works in Word 2003, but it will not work for
all tables in a larger document. I'm not sure why, and was wondering
if anyone had any ideas.

This is probably because your code is way too complicated and uses the
selection object. Avoid the selection object as much as possible as it is
unreliable and slower.
Try something like:

Sub ConvertTablesToOLE()
Dim rgeTable As Range

With ActiveDocument
Do While .Tables.Count > 0
'Set range to first table and cut table
Set rgeTable = .Tables(1).Range
.Tables(1).Range.Cut

'Create OLE
rgeTable.PasteSpecial Placement:=wdInLine, _
DataType:=wdPasteOLEObject
Loop
End With

End Sub

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hello all,

I've got a VBA function that attempts to take a word table, select it,
cut it, and then create a Word OLE object where the table used to be,
then pastes the table into the Word OLE. I need this for a program I
work with that doesn't play nice with Word tables.

The code is below, and it works in Word 2003, but it will not work for
all tables in a larger document. I'm not sure why, and was wondering
if anyone had any ideas.

Another reason why it fails on all tables is because every time you convert
a table in OLE object, the table count in the document is lessen by one, as
you have noticed.
Let's say there are 5 tables in the document;
before the first pass
i=1
tCount =5
0 table processed
after one pass
i=2
tCount =4
1 table processed

after two passes
i =3
tCount =3
2 table processed

after three passes
i =4
tCount =2
3 table processed
The loop stops because now i > tCount (you used: While i <= tCount)

So this means that 2 tables were not processed.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

kbmurphy

Jean-Guy,

Merci beaucoups!

I appreciate the suggestion and will get if a shot. It's more than not
looping through correctly, though, because I had a document with 10
tables and it only did 2 of them and quit.

Thanks for getting me on the right track.

Kev
 

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