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

Find something in a MS Word paragraph with VBA 1

Status
Not open for further replies.

lewatkin

Programmer
Mar 8, 2002
91
US
Hey gang! I know there is someone here that can help me. I have 10,000+ MS Word Docs. They are all formatted the same - another program creates them. See below:

"Commencing at NGS monument "Any High 2" having XXXX Grid Coordinates (NAD 83-2001) X=XXX.XX and X=XXXX; thence from said point XXX XX ° XX ' XX " XXX, XXX feet to an existing iron pin and being the POINT OF BEGINNING; thence from said POINT OF BEGINNING the following 5 courses and distances: XXX XX ° XX ' XX " XXX, XXX feet; thence XXX XX ° XX ' XX " XXX, XXX feet; thence XXX XX ° XX ' XX " XXX, XXX feet; thence XXX XX ° XX ' XX " XXX, XXX feet; thence XXX XX ° XX ' XX " XXX, XXX feet
to the POINT OF BEGINNING; containing 0.039 acres (1683 sq. ft.) more or less, according to a plat entitled "Right of Way and Easement Map for XXXX" by XXXX of XXXX, Inc., dated March 14, 2008."

Out of that paragraph, I need to be able to extract the number of sq. ft. (for this one 1683).

I am extracting several pieces of information from the entire document and placing them in a MS Access database. This is the final piece and I am not having any luck. Code I am using is below (well it is only a piece of the code):


.GoTo what:=wdGoToLine, Which:=wdGoToFirst, Count:=16
With Selection.Find
.Forward = True
.ClearFormatting = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindContinue
.Execute findtext:="sq. ft.)"
End With

So with that code, I can find "sq. ft.)", but I need the number before it. If anyone can help I would graciously appreciate it!!!
 
I do not know what you are doing with "I am extracting several pieces of information from the entire document", but if you are using Selection - as it seems youi are - it could probably be done better.

In any case, take a look at the following code. I hope it helps.
Code:
Option Explicit

Sub yaddaYadda()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .Text = "sq. ft."
   .Execute
End With
   MsgBox r.Words(1).Previous(Unit:=wdWord, Count:=1)
End Sub
It makes a range object of the document, and using .Find, executes a search for "sq. ft.". It then returns the string value of the .Previous word - i.e. "1683" - to a messagebox. Obviously you would want to do something else with that value. Also, to make sure there IS a "sq. ft." to be found, best practices would put in some error trapping. There is no error trapping in the example above.

Try to avoid use Selection. It is much much better to use Range.

If you happen to know the specific paragraph (say it is always going to be the third paragraph), then you could go about it a different way.

Gerry
 
Gerry,

Thanks so much for the code. It worked fine on the first document in the folder, but when it went to the second document, it failed on Set r = ActiveDocument.Range. Not sure why. Nonetheless, I am using Selection and it is working fine. The code is not pretty at all, but it works. Thanks for your time. Have a star for your effort!!

 
1. "It worked fine on the first document in the folder, but when it went to the second document, it failed on Set r = ActiveDocument.Range. "

Well, considering you give zero information on how, exactly, you are processing through the files in the folder (FSO??? Dir function???), I can not suggest anything. Properly done, using a range object does not fail, and I regularly process hundreds/thousands of files.

2. "Not sure why. Nonetheless, I am using Selection and it is working fine. "

My bolding. Ummm, possibly because you ARE using Selection.

I am not sure how you can state it is working fine, and also state something is failing.

Shrug. Anyway, something is working I guess. Again, I am not aware of how you are processing these files, but I hope you are using Document objects. I hope you are clearing out any memory chunks, because Word can get rather funky with that kind of large scale processing.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top