?
Solved

How do I search for text in a MS document using vb.net and then return the 7 characters adjacent to the found text?

Posted on 2014-07-16
4
Medium Priority
?
1,056 Views
Last Modified: 2014-07-18
Hi,
I have been trying, with very little success, to search for a string in an MS Word document using VB.net and then move to the end of that string, select the next 7 characters and return them as a string. I have tried various permutations of the selection and range methods and properties but I can't find a way to move to the end of the found string, extend the range/selection for 7 characters and then return those 7 characters.

Any help would be very much appreciated,

Thanks
0
Comment
Question by:BozM
  • 2
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 40200333
We don't know where you are stuck, but here is a VBA function to do that job:
Function GetNext7(Doc As Word.Document, strSearchString As String) As String
    Dim rng As Word.Range
    
    Set Doc = ActiveDocument
    Set rng = Doc.Range
    With rng.Find
        .Text = strSearchString
        If .Execute() Then
            rng.Collapse wdCollapseEnd
            rng.End = rng.End + 7
            GetNext7 = rng.Text
        End If
    End With
End Function

Open in new window

0
 

Author Comment

by:BozM
ID: 40204061
Thanks GrahamSkan, I am not sure how much of that functionality is available in vb.net but I will try and mirror in vb.net code and come back to you.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40204186
I think that it will work more-or-less as is, except for line 4 which I should have removed before posting the code.
0
 

Author Comment

by:BozM
ID: 40204232
Thanks, I have tricked around a little with it and I have got it working. It looks like this (it's for a Word Addin in Add-in Express so it's slightly different than usual vb.net code). The only change I needed to make was to the wdcollapse unit.

Really appreciate your help on this. Thanks again.

  Public Function GetDocNoFromDoc() As String
        Dim strDocNoFromDoc As String
        Dim rng As Word.Range

        rng = WordApp.ActiveDocument.Range

        With rng.Find
            .ClearFormatting()
            .Text = "DocNo:"
            If .Execute() Then
                rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                rng.End = rng.End + 7
                strDocNoFromDoc = rng.Text
            End If
        End With

        Return strDocNoFromDoc
    End Function

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 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