Read each line of text

R

Robert Crandal

This should be an easy task, but I'm not very skilled with
VBA for Word 2010:

I need VBA code that will read each and every line of a
Word document. (Empty lines containing only whitespace
characters should be ignored.) Here is an example of the
type of document I am reading:

zid100 100 East Broadway
zid120 120 North Palisade Drive
zid200 200 E. Filmore Street

etc.. etc...

When I read each line, is there a way to parse the first
token into a string variable, and put the street string in
a second different variable name?? For example, in the
first line, a variable named $MyID will store "zid100",
and a second variable named $MyStreet will store
the street address of "100 East Broadway", etc. etc...??

Thanks.

Robert
 
E

Ed Weber

I assume that you mean each paragraph, not each line.
The $ designator must be at the end of the variable name, not the beginning.
Depending on what you are doing, it might be more efficient to assign the ID
and the street to array variables rather than simple variables.

Below are two ways of doing the same thing

Sub SpltLines1()
Dim oPara As Paragraph
Dim strText As String
strText = ""
For Each oPara In ActiveDocument.Paragraphs
strText = oPara.Range.Text
If strText <> vbCr Then
MyID$ = Trim(Mid(strText, 1, InStr(strText, " ")))
Mystreet$ = Trim(Mid(strText, InStr(strText, " ")))
MsgBox "MyID$ = " & MyID & vbCr & "MyStreet$ = " & Mystreet
strText = ""
End If
Next
End Sub

Sub Splitlines2()
Dim oPara As Paragraph
Dim strText() As String
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Text <> vbCr Then
strText = Split(oPara.Range.Text, " ", 2)
MsgBox "ID = " & strText(0) & vbCr & "Street = " & strText(1)
End If
Next


--


Robert Crandal said:
This should be an easy task, but I'm not very skilled with
VBA for Word 2010:

I need VBA code that will read each and every line of a
Word document. (Empty lines containing only whitespace
characters should be ignored.) Here is an example of the
type of document I am reading:

zid100 100 East Broadway
zid120 120 North Palisade Drive
zid200 200 E. Filmore Street

etc.. etc...

When I read each line, is there a way to parse the first
token into a string variable, and put the street string in
a second different variable name?? For example, in the
first line, a variable named $MyID will store "zid100",
and a second variable named $MyStreet will store
the street address of "100 East Broadway", etc. etc...??

Thanks.

Robert




---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 130204-0, 02/04/2013
Tested on: 2/4/2013 9:48:34 AM
avast! - copyright (c) 1988-2013 AVAST Software.
http://www.avast.com
 

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