Word

Convert Table To Text And Convert Text To Table

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

MOS MASTER

Description:

The sub ConvertTableToText turns the bookmarked table to tab delimited text. The sub ConvertTextToTable turns the bookmarked range (Tab delimited) to a table both subs have a check to look for the correct table count in the bookmarked range. 

Discussion:

Converting a delimited range to a table can be very handy at times. And sometimes you need the opposite to happen. (Convert table to text) Both subs use a bookmark to locate the right area to work on. 

Code:

instructions for use

			

Option Explicit Sub ConvertTableToText() Dim oTable As Word.Table ' \\ If bookmarked table count not 1 then exit sub If ActiveDocument.Bookmarks("bmTable").Range.Tables.Count _ <> 1 Then Exit Sub ' \\ Get a handle to the bookmarked table Set oTable = ActiveDocument.Bookmarks("bmTable").Range.Tables(1) ' \\ Convert it to text separated by Tabs oTable.ConvertToText Separator:=wdSeparateByTabs ' \\ Clean up Set oTable = Nothing End Sub Sub ConvertTextToTable() Dim oRange As Word.Range ' \\ If bookmarked table count not 0 then exit sub If ActiveDocument.Bookmarks("bmTable").Range.Tables.Count _ <> 0 Then Exit Sub ' \\ Get a handle to the bookmarked range Set oRange = ActiveDocument.Bookmarks("bmTable").Range ' \\ Convert it to a table separated by Tabs oRange.ConvertToTable Separator:=wdSeparateByTabs ' \\ Clean up Set oRange = Nothing End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. From Word, press Alt + F8 to open the macro dialog box.
  2. Select ConvertTableToText to convert a table to text
  3. Click Run.
  4. From Word, press Alt + F8 to open the macro dialog box.
  5. Select ConvertTextToTable to convert text to a table
  6. Click Run.
 

Sample File:

Convert table to text.zip 11.24KB 

Approved by mdmackillop


This entry has been viewed 139 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express