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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word RTF Parsing Macro

Status
Not open for further replies.

dirwin

Programmer
Oct 19, 2004
3
US
Hi all,

I'm an experienced programmer, but I'm a complete newbie in the realm of VBA. I've got a problem that I can't seem to answer with my combination of books/google/help files, and I searched here and couldn't find an answer either. I'm writing a macro to parse through an RTF file making formatting changes on certain text selections. I'd like to begin at the top, find a hard-coded key word, perform some Selection.Style and Selection.Paragraphs operations, and then move on to the next instance of the keyword. I can do all of this, but I don't know the proper way to handle loop control. Currently I just have a dummy Do/While loop (more or less as proof of concept.) My pseudocode solution looks like this:
Code:
Selection.Find {bunch of find parameters}
Selection.Find.Execute
Do While NOT END OF FILE
   IF (Selection contains keyword) THEN
      Format Selection properly
   END IF
Loop
Currently, instead of "Do While NOT END OF FILE", I have "DO WHILE lcv < 500" and then I increment lcv (loop control variable) each time through the loop.

Obviously, the macro is more complicated than that (or else I'd just use the replace all and skip the macro altogether), but that's the top-level overview. My problem is that I don't know how to use EOF in VBA. The actual EOF function seems to require a file number which requires opening a file in the macro, which just doesn't work. I'd like to either break out of the loop at the end of file, or count the number of keywords and use that as my lcv.

This should be relatively simple, but I've failed at finding a fix in the documentation anywhere. Any help would be greatly appreciated.

Thanks in advance =)

--Dan
 
Starting point, from the help file:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleHeading3
[highlight]Do While .Execute[/highlight](FindText:="", Forward:=True, _
Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "Tip: "
.Move Unit:=wdParagraph, Count:=1
End With
Loop
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The actual EOF function seems to require a file number which requires opening a file in the macro, which just doesn't work.

I am not quite understanding the above. You are using the Selection object, which means the activedocument. The file IS open. What do you mean "requires opening a file in the macro, which just doesn't work?

Gerry
 
You are using the Selection object, which means the activedocument. The file IS open. What do you mean "requires opening a file in the macro, which just doesn't work?

The EOF Function takes a file number as a parameter. I'm not aware of a way to set a file number for the currently open file (EOF seems more geared toward parsing an external data file or something like that.)

Documenation for EOF:

EOF(filenumber)

The required filenumber argument is an Integer containing any valid file number.

Example:
Code:
Dim InputData
Open "MYFILE" For Input As #1    ' Open file for input.
Do While Not EOF(1)    ' Check for end of file.
    Line Input #1, InputData    ' Read line of data.
    Debug.Print InputData    ' Print to the Immediate window.
Loop
Close #1    ' Close file.
 
Thanks for your time guys, but I think I've reached a decent solution. I decided to just compare the page number of the current page being analyzed with the last page number and stop when the last page number is greater than the current one (ie. when the search loops back to the top.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top