The ConvertToText Method

This method converts the text within a table into plain text and returns a Range object that represents that text. The syntax is:

expression.ConvertToText(Separator)

where expression refers to a Row, Rows, or Table object. The optional Separator parameter specifies the character that delimits the text within each row (that is, it delimits the columns). It can be any of the constants in the following enum:

	Enum WdTableFieldSeparator 
	   wdSeparateByParagraphs = 0 
	   wdSeparateByTabs = 1 
	   wdSeparateByCommas = 2 
	   wdSeparateByDefaultListSeparator = 3
	End Enum

It is important to note that when the ConvertToText method is applied to a Table object, the table is deleted. For instance, the following code:

	Set tbl = ActiveDocument.Tables(1)
	Set rng = tbl.ConvertToText(wdSeparateByCommas)
	MsgBox tbl.Columns.Count

produces the error message “Object has been deleted” when it gets to the third line, since the object referred to by tbl has been deleted.

This brings up the question of how to test whether an object reference, such as tbl, is still valid—that is, still refers to a valid object. Your first thought might be to use the Nothing keyword, discussed in Chapter 8, Control Statements, to write:

	If tbl Is Nothing Then ...

However, this returns False, even though the object has been deleted, so it does not help. Instead, you must use the IsObjectValid property. This property is global, so you can test whether the tbl object still exists by writing:

 If IsObjectValid(tbl) Then ... ...

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.