[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VBA search paragraph and extract block of text

Posted on 2009-07-02
10
Medium Priority
?
764 Views
Last Modified: 2012-05-07
Hello, I am running an Access VBA subroutine that opens a word file, searches the file for some tokens, and extracts those texts until the closing token appears. I am new to word programming but not to excel or access. Anyone have any examples or solutions? Thanks
0
Comment
Question by:allenlo77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:Berkson Wein
ID: 24767493
Is the start "token" the same as the end token?
Is there more than one set of start and end tokens?
I'll whip up some code, just let me know.  Won't take but a minute.
0
 

Author Comment

by:allenlo77
ID: 24767506
could be, doesnt have to be, your call!
0
 
LVL 15

Accepted Solution

by:
Berkson Wein earned 2000 total points
ID: 24767890
This will spit out the found text in a msgbox and assigns it to a variable.
I used ">>" as the starting token and ""<<" as the ending token.  If this isn't what you're using, change the
.Text = "\>\>*\<\<"
line to be the characters you're using.  You need to escape (\) special characters.
Also, at the end, where I trimmed of the tokens using left and right, you'll need to use the length of the starting token to replace the 2 and the total length of the start and end token to replace the 4.
 
This should do what you need.  You can loop if you need to find multiple occurrences.
Hope this helps.

Sub Find()
    Dim sFoundText As String
    
    Selection.Find.ClearFormatting
    
    With Selection.Find
        .Text = "\>\>*\<\<"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    sFoundText = Left(Right(Selection.Text, Len(Selection.Text) - 2), Len(Selection.Text) - 4)
    MsgBox sFoundText
End Sub

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:allenlo77
ID: 24772166
weinberk, I am trying to extract a whole paragraph, wiill this work?

For example, the doc file has this

fdksljfkldjlkj >> hello my name is allen, blah blah

new paragraph blah blah blah

new paragraph blah blah. Okay we are done <<

And I'd like to extract everything between the '>>' token and '<<' token. I am looking at the code you posted and I am not even sure if it will work.
0
 
LVL 15

Expert Comment

by:Berkson Wein
ID: 24772266
Yes it works.  
I pasted your sample text into a word document then ran the code I provided.  A message box pops up with the text (although boxes appear where carriage returns are since the msg box doesn't support returns like that), and the entire text is available in the variable to do whatever you want with.
0
 

Author Comment

by:allenlo77
ID: 24772452
Ok it works, how do you handle white space between paragraphs? I have 4 paragraphs all separated by white space line.
0
 
LVL 15

Expert Comment

by:Berkson Wein
ID: 24772626
You just asked to return the text, which is why my code does.  
What is it that you want the code to do with the "white space?"  In your example text, it's just returns.  This might be another topic....
0
 

Author Comment

by:allenlo77
ID: 24772645
well because there is white space between the paragraphs, it gives me an "Invalid procedure call or argument", so I was wondering if you have a solution for that
0
 

Author Comment

by:allenlo77
ID: 24772722
nevermind, it works, the mouse cursor cannot be on the paragraph in between the tokens or else it doesn't work
0
 
LVL 15

Expert Comment

by:Berkson Wein
ID: 24772987
You're right.  Replace
Selection.Find.ClearFormatting

with
 ActiveDocument.Content.Select
and give it a whirl.  
Please don't forget to accept a solution.  I've worked hard to get a solution for you.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question