extracting data from Mailing list?

P

Piet Linden

I am an Access programmer and at best a hack at Word programming...

I have a document that someone created as a mailing list, so it's
essentially a 5-column table.
I can loop through the rows/columns of the table, but I can't figure
out how to determine if the cell is empty. IsNull fails, IsEmpty
fails.

How do I check for just a paragraph marker? not wdParagraph, not
vbcrlf.... I was thinking that if I compared to that and failed, then
the cell has an address in it.

What is the constant or whatever I need to check against?

Thanks!
Pieter
 
M

macropod

Hi Piet,

To extract just the cell contents without the cell delimiter, you'd use something like:
Dim oRng as Range
Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
oRng.MoveEnd Unit:=wdCharacter, Count:= -1

If the cell is empty, oRng.Txt will return "".
 
P

Piet Linden

Hi Piet,

To extract just the cell contents without the cell delimiter, you'd use something like:
Dim oRng as Range
Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
oRng.MoveEnd Unit:=wdCharacter, Count:= -1

If the cell is empty, oRng.Txt will return "".

MacroPod,

Thanks a mil! Got *most* of it working....

here's the code (and try not to laugh!)

Private Sub Command0_Click()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim iCounter As Integer
Dim iRow As Integer, iColumn As Integer
Dim oRng As Range
Dim strContents As String
Dim rsAddr As DAO.Recordset

'Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
'oRng.MoveEnd Unit:=wdCharacter, Count:=-1

Set appWord = New Word.Application
appWord.documents.Open "C:\AddressList.doc"
Set docWord = appWord.ActiveDocument

Set rsAddr = DBEngine(0)(0).OpenRecordset("Addressbook",
dbOpenTable, dbAppendOnly)

For iRow = 1 To docWord.Tables(1).Rows.Count
For iColumn = 1 To docWord.Tables(1).Columns.Count
Set oRng = docWord.Tables(1).Cell(iRow, iColumn).Range
If iRow = docWord.Tables(1).Rows.Count And iColumn =
docWord.Tables(1).Columns.Count Then
oRng.MoveEnd unit:=wdCharacter, Count:=-1
Else
oRng.MoveEnd unit:=wdCharacter, Count:=-2
End If

strContents = Trim(oRng.Text)

If strContents <> "" Then
'iCounter = iCounter + 1
rsAddr.AddNew
rsAddr.Fields("Address") = strContents
rsAddr.Update
End If
Next iColumn
Next iRow

rsAddr.Close
appWord.ActiveDocument.Close

Set rsAddr = Nothing
Set docWord = Nothing
appWord.Quit
Set appWord = Nothing

'MsgBox "Job's done", vbOKOnly
DoCmd.OpenTable "Addressbook", acViewNormal
End Sub

The part that fails is that it crops the last character off the last
non-empty cell's contents and then inserts another one with non-
printable characters. It looks like the label template has some extra
spaces in it or something. Any idea how to deal with that part?

Oh, near forgot... what's the delimiter between lines inside the
individual cells? I need to use it to parse the label into separate
lines.

Thanks!

Pieter
 
M

macropod

Hi Piet,

Try this:
Private Sub Command0_Click()
Dim appWord As Word.Application
Dim iCounter As Integer
Dim oRng As Range
Dim oTbl As Table
Dim oCel As Cell
Dim rsAddr As DAO.Recordset
Dim strContents As String
Set appWord = New Word.Application
appWord.Documents.Open "C:\AddressList.doc"
Set rsAddr = DBEngine(0)(0).OpenRecordset("Addressbook", dbOpenTable, dbAppendOnly)
iCounter = 0
With appWord.ActiveDocument
For Each oTbl In .Tables
For Each oCel In oTbl.Range.Cells
Set oRng = oCel.Range
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
Do While Asc(.Characters.Last) = 11 Or Asc(.Characters.Last) = 13 Or _
Asc(.Characters.Last) = 32 Or Asc(.Characters.Last) = 160
.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
Do While Asc(.Characters.First) = 11 Or Asc(.Characters.First) = 13 Or _
Asc(.Characters.First) = 32 Or Asc(.Characters.First) = 160
.MoveStart Unit:=wdCharacter, Count:=1
Loop
End With
strContents = oRng.Text
If strContents <> "" Then
iCounter = iCounter + 1
With rsAddr
.AddNew
.Fields("Address") = strContents
.Update
End With
End If
Next oCel
Next oTbl
End With
rsAddr.Close
appWord.ActiveDocument.Close
Set rsAddr = Nothing
Set docWord = Nothing
appWord.Quit
Set appWord = Nothing
MsgBox "Job's done", vbOKOnly
DoCmd.OpenTable "Addressbook", acViewNormal
End Sub

Three things worth noting are that the code:
1. processes all tables in the document - you may want to change that.
2. loops through the cells collection rather than going by row & column. It can be done this way because Word processes the cells
collection by column within row anyway.
3. uses two loops to strip off any leading & trailing spaces (Chr 32 & Chr 160) and linefeeds/carriage returns (Chr 11 & Chr 13).
Apart from meaning you don't then need to trim the result, doing it this way also takes of any combination/permutation of those
leading & trailing characters.

--
Cheers
macropod
[MVP - Microsoft Word]


Hi Piet,

To extract just the cell contents without the cell delimiter, you'd use something like:
Dim oRng as Range
Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
oRng.MoveEnd Unit:=wdCharacter, Count:= -1

If the cell is empty, oRng.Txt will return "".

MacroPod,

Thanks a mil! Got *most* of it working....

here's the code (and try not to laugh!)

Private Sub Command0_Click()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim iCounter As Integer
Dim iRow As Integer, iColumn As Integer
Dim oRng As Range
Dim strContents As String
Dim rsAddr As DAO.Recordset

'Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
'oRng.MoveEnd Unit:=wdCharacter, Count:=-1

Set appWord = New Word.Application
appWord.documents.Open "C:\AddressList.doc"
Set docWord = appWord.ActiveDocument

Set rsAddr = DBEngine(0)(0).OpenRecordset("Addressbook",
dbOpenTable, dbAppendOnly)

For iRow = 1 To docWord.Tables(1).Rows.Count
For iColumn = 1 To docWord.Tables(1).Columns.Count
Set oRng = docWord.Tables(1).Cell(iRow, iColumn).Range
If iRow = docWord.Tables(1).Rows.Count And iColumn =
docWord.Tables(1).Columns.Count Then
oRng.MoveEnd unit:=wdCharacter, Count:=-1
Else
oRng.MoveEnd unit:=wdCharacter, Count:=-2
End If

strContents = Trim(oRng.Text)

If strContents <> "" Then
'iCounter = iCounter + 1
rsAddr.AddNew
rsAddr.Fields("Address") = strContents
rsAddr.Update
End If
Next iColumn
Next iRow

rsAddr.Close
appWord.ActiveDocument.Close

Set rsAddr = Nothing
Set docWord = Nothing
appWord.Quit
Set appWord = Nothing

'MsgBox "Job's done", vbOKOnly
DoCmd.OpenTable "Addressbook", acViewNormal
End Sub

The part that fails is that it crops the last character off the last
non-empty cell's contents and then inserts another one with non-
printable characters. It looks like the label template has some extra
spaces in it or something. Any idea how to deal with that part?

Oh, near forgot... what's the delimiter between lines inside the
individual cells? I need to use it to parse the label into separate
lines.

Thanks!

Pieter
 
P

Piet Linden

MacroPod,

Thanks for the help... I tried the code modifications and my computer
freezes... (It's a feature!) I'll have to test it later to see what's
going on... Thanks for the feedback, though!

Pieter
 
M

macropod

Hi Piet,

I suspect the freezing is to do with the code I posted not working correctly if the cell is empty. You'll need to add the line:
If Len(oRng.Text) > 0 Then
after the first '.MoveEnd Unit:=wdCharacter, Count:=-1' line and the line:
End If
after the second 'Loop' to prevent the code 'freezing' if the cell is empty.

I'm also not familiar with the Access model and whether it's possible that 'Dim oTbl As Table' is being interpreted as a reference
to an Access table rather than a Word table. If so, changing to 'Dim oTbl As Word.Table' or just 'Dim oTbl' should work.
 
M

macropod

Hi Piet,

You might also need to add:
If Len(oRng.Text) = 0 Then Exit Do
before each of the two 'Loop' lines to take care of any oRng strings whose contents get reduced to nothing.
 
P

Piet Linden

Hi Piet,

You might also need to add:
If Len(oRng.Text) = 0 Then Exit Do
before each of the two 'Loop' lines to take care of any oRng strings whose contents get reduced to nothing.

--
Cheers
macropod
[MVP - Microsoft Word]

Piet Linden said:
MacroPod,
Thanks for the help... I tried the code modifications and my computer
freezes... (It's a feature!)  I'll have to test it later to see what's
going on...  Thanks for the feedback, though!

Macropod,

Works a CHAMP!!! Last question... what do I use to parse the
individual lines in the mailing label? Inside the label it *looks
like* a manual line break or similar... I would use Split() in
Access, but is there a better way to do it in Word that works better?

Thanks!
 
M

macropod

Hi Piet,

Using Split would be fine if you want each line to go into a separate field. However, if you simply want to get rid of the line
feeds, you could use Replace, with the replacement character of your choice.

--
Cheers
macropod
[MVP - Microsoft Word]


Hi Piet,

You might also need to add:
If Len(oRng.Text) = 0 Then Exit Do
before each of the two 'Loop' lines to take care of any oRng strings whose contents get reduced to nothing.

--
Cheers
macropod
[MVP - Microsoft Word]

Piet Linden said:
MacroPod,
Thanks for the help... I tried the code modifications and my computer
freezes... (It's a feature!) I'll have to test it later to see what's
going on... Thanks for the feedback, though!

Macropod,

Works a CHAMP!!! Last question... what do I use to parse the
individual lines in the mailing label? Inside the label it *looks
like* a manual line break or similar... I would use Split() in
Access, but is there a better way to do it in Word that works better?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top