Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2000: Loop until end of document? 1

Status
Not open for further replies.

AMadden

Technical User
Mar 22, 2002
16
US
This question pertains to Microsoft Word 2000. I am writing a VBA routine and need to loop until the end of document is reached. I have looked for examples online but don't see any good examples. I want to use the construct similar to:

Do
.....
Loop until 'END OF DOCUMENT'

Any help with this would be apprecitated.
---------------------------------
Art Madden
amadden@tampabay.rr.com
 
this example will loop through all paragraphs in all story ranges in a word document

Code:
Option Explicit

Sub test()
  Dim oPara As Paragraph
  Dim oStory As Range
  Dim oDOC As Document
  
  Set oDOC = ActiveDocument
  For Each oStory In oDOC.StoryRanges
    For Each oPara In oStory.Paragraphs
      Debug.Print Left$(oPara.Range.Text, Len(oPara.Range.Text) - 1)
    Next oPara
  Next oStory
  Set oDOC = Nothing
  
  MsgBox "Done"
End Sub
 
I am actually trying to parse a data file and delimit it with commas and semicolons (commas for field separators and semicolons for end-of-record markers).

The data file is currently delimited with paragraph markers between each field and two consecutive paragraph markers at the end of records.

I tried to do a simple "search and replace" operation, replacing all instances of two consecutive paragraph markers with semicolons, then replacing single paragraph markers with commas. This did not work because some records do not contain data and have two paragraph markers within a record.

Since each record contains only three fields: First Name, Last Name, and State, my plan was to write a VBA routine that would start at the beginning of the document and perform the following actions until the end of document is reached:

1) skip past the first two paragraph marks
2) search for the next instance of two consecutive paragraph marks and replace with a semicolon
3) Loop through operations 1 & 2 until the end of document is reached.

If this loop is repeated through the entire document it should properly mark the end of each record, enabling me to finish by doing a simple search and replace of remaining paragraph markers with commas to properly delimit the file.

The following is a sample of the data file structure. Note that in the last record that the first name is missing, resulting in two consecutive paragraph markers within a record.

John^p
Smith^p
FL^p
^p
Mark^p
Johnson^p
GA^p
^p
Steve^p
Allen^p
IN^p
^p
^p
Johnson^p
GA^p
^p

Thanks for the help. I normally program with VBA in Excel or Access and am not proficient with the Word object model.

---------------------------------
Art Madden
amadden@tampabay.rr.com
 
Hello Art,

This seemed to work for me.

Public Sub Test()
Application.DisplayAlerts = wdAlertsNone
Selection.WholeStory
Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=4, _
NumRows:=5, Format:=wdTableFormatNone, ApplyBorders:=True, ApplyShading _
:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:=False, _
AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed
Selection.Rows.ConvertToText Separator:=wdSeparateByCommas, NestedTables:= _
True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", ^p"
.Replacement.Text = ";"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.DisplayAlerts = wdAlertsAll
End Sub

Hope this helps,
Pete
 
Thanks Pete. That's just what I needed... ---------------------------------
Art Madden
amadden@tampabay.rr.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top