NullVoxPopuli
NullVoxPopuli

Reputation: 65103

Word 2007 - How do I convert text with inconsistent tabbing to a table?

I have a whole bunch of tables that were formatted weird, because when a bunch of people make tables out of tabs, one cannot expect the tabbing to be consistent on all of them.

For example... I have this table: 2 cols by 11 rows... but I would like a macro that didn't care about the number of columns and rows

space space space **column header** tab tab tab tab **column header**
tab **data** tab tab tab **data** tab tab
tab **data** tab tab **data**
tab **data** tab tab tab tab **data**
tab **data** tab tab **data**

Currently, I have this macro

Sub ConvertTextToATable()

     Selection.ConvertToTable Separator:=wdWhite, AutoFitBehavior:=wdAutoFitFixed

End Sub

I've tried the tab separator... but both give similar results. It creates a new column every time a new white space character is encountered. What I want is ignoring all white space, and creating a new cell every time text is encountered, and creating a new row every time \n or \r is encountered.

And since I can't guarantee that every table with have the same number of column headers as the data, I think the number of columns should be determined by the second or third row in the text of original pseudo table.

Upvotes: 1

Views: 1301

Answers (1)

Segfault
Segfault

Reputation: 8290

DerNalia, this calls for a Regular Expression. Add a reference to Microsoft VBScript Regular Expressions 5.5 in your Office VBA editor: Image of VBA references
(source: david at wbtn.net)

Then, in your Macro use the Regex objects to replace all multiple instances of a tab with just one tab. Then you can use your ConvertToTable command like normal.

Upvotes: 1

Related Questions