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

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
BozMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BozMAuthor Commented:
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
GrahamSkanRetiredCommented:
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
BozMAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.