Sub
ConvertLabelsToData()
Dim oDoc As
Document, oNewDoc As Document
Dim oSection As
Section
Dim oTable As
Table
Dim ocell As Cell
Dim oPara As
Paragraph
Dim oRng As Range
'The process could
take a while, so warn the user
MsgBox "With a large label file, this macro will take a long time" & vbCr &
_
"to run. Please wait until the task completed message is displayed", _
vbInformation, "Labels to Data"
'Turn off screen
updating
Application.ScreenUpdating = False
Set oDoc =
ActiveDocument
'Create a new
document to take the data
Set oNewDoc = Documents.Add
'Check each table in
each section of the document
For Each oSection In
oDoc.Sections
For Each oTable
In oSection.Range.Tables
For Each ocell In oTable.Range.Cells
Set oRng = ocell.Range
oRng.End = oRng.End - 1
'Replace any line breaks in the cell with paragraph
breaks
oRng = Replace(oRng, Chr(11), Chr(13))
'Replace the paragraph breaks with a field end marker
'|'
oRng = Replace(oRng, Chr(13), Chr(124))
'Copy the range to the end of the new document
oNewDoc.Range.InsertAfter oRng & vbCr
Next ocell 'and process the next cell
Next oTable
Next oSection
'We have finished with the
label document so close
'without saving
oDoc.Close wdDoNotSaveChanges
'Check each
paragraph in the new document
For Each oPara In
oNewDoc.Paragraphs
'Delete any short paragraphs
If Len(oPara.Range) <
3 Then
oPara.Range.Delete
End If
'If the paragraph
begins with the chosen field end character '|'
'Delete the character
If oPara.Range.Characters.First = Chr(124)
Then
oPara.Range.Characters.First.Delete
End If
Next oPara
'Sort the data into
alphabetical order
oNewDoc.Range.Sort
'Remove any superfluous
spaces and field end characters
'that may be present
at the end of each paragraph
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[| ]{1,}^13"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
'Convert the text to
a table
oNewDoc.Range.ConvertToTable Chr(124)
'Add a header row to
the table
oNewDoc.Tables(1).Rows(1).Select
Selection.InsertRowsAbove NumRows:=1
'Add field names to
the header row
oNewDoc.Tables(1).Cell(1, 1).Range.Text = "Name"
For i = 2 To
oNewDoc.Tables(1).Columns.Count
oNewDoc.Tables(1).Cell(1, i).Range.Text = "Address" & i
Next i
'Restore screen updating
Application.ScreenUpdating =
True
'Job done, so tell the
user
MsgBox "Data complete - be sure to check for duplicate entries", _
vbInformation, "Labels to Data"
End Sub