none
how to extract a specific set of lines from word document RRS feed

  • Question

  • Hello

    i am try to do the below in sequence using vbscript

    1. Open a word document

    2. locate all the headings of style H3 and store in an array

    3. get the line and page numbers of each heading and store in an array

    ......till here i am successful in doing it, but in the next step i am stuck..

    4. get the content from each heading (don't know how to tackle this)

    Also, since i have the line/page number of each heading, i tried to access the content in that range but unsuccessful.

    Any help to tackle step 4 would be much appreciated.

    Thanks you.

    '*******************************************************************************************************

    'Function to search for a string (Heading of style H3) in the word document and echo the line and page number

    'This function accepts a string

    Function StringSearch_Range(strExpectedString)
    blnFlag = False
    With currentDocument
    For p = 1To .Paragraphs.Count
    startRange = .Paragraphs(p).Range.Start
    endRange = .Paragraphs(p).Range.End
    Set tRange = .Range(startRange, endRange)
    tRange.Find.ClearFormatting
    tRange.Find.MatchPhrase = True
    tRange.Find.MatchCase = True
    tRange.Find.Forward = True
    tRange.Find.IgnoreSpace = True
    tRange.Find.Wrap = wdFindContinue
    tRange.Find.Text = strExpectedString
    tRange.Find.Execute
    If (tRange.Find.Found) Then
    blnFlag = True
    MsgBox"Found at Line number: " & tRange.Information(wdFirstCharacterLineNumber) & " in the page number :" & tRange.Information(wdActiveEndPageNumber)
    ExitFor
    Else
    MsgBox"Not Found"
    EndIf
    Next
    EndWith
    Set tRange = Nothing
    EndFunction


    • Edited by ashokkn Wednesday, July 4, 2012 12:04 PM
    Wednesday, July 4, 2012 12:01 PM

Answers

  • Some remarks:

    • The active document is ActiveDocument, not currentDocument.
    • Your code searches for a text string, not for headings with style H3.
    • The lines
    startRange = .Paragraphs(p).Range.Start
    endRange = .Paragraphs(p).Range.End
    Set tRange = .Range(startRange, endRange) 

           are equivalent to

    Set tRange = .Paragraphs(p).Range
    • Your code loops through the paragraphs, then search each paragraph for the text string. Why not search the entire document for the text string? That would be much more efficient.
    • The text of a range tRange is tRange.Text

    Regards, Hans Vogelaar

    • Marked as answer by ashokkn Thursday, July 5, 2012 5:46 AM
    Wednesday, July 4, 2012 1:43 PM

All replies

  • Some remarks:

    • The active document is ActiveDocument, not currentDocument.
    • Your code searches for a text string, not for headings with style H3.
    • The lines
    startRange = .Paragraphs(p).Range.Start
    endRange = .Paragraphs(p).Range.End
    Set tRange = .Range(startRange, endRange) 

           are equivalent to

    Set tRange = .Paragraphs(p).Range
    • Your code loops through the paragraphs, then search each paragraph for the text string. Why not search the entire document for the text string? That would be much more efficient.
    • The text of a range tRange is tRange.Text

    Regards, Hans Vogelaar

    • Marked as answer by ashokkn Thursday, July 5, 2012 5:46 AM
    Wednesday, July 4, 2012 1:43 PM
  • Many thanks Hans.

    Now i am able to successfully rip the contents under each search string.


    Thank you Ashok

    Thursday, July 5, 2012 5:47 AM