O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ... ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required