Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieving text from a page in Word (VB/Word)

    Hi guys

    I am trying to retrieve the text that contains a Word document. With Powerpoint, I know that I can select a slide and retrieve easily all the text using the properties TextFrame.TextRange.Text for each shape inside the slide. However, I'm completely lost in Word.

    So far, I've been able to know when a page ends using the following ( I don't know if this is the best way):

    Set oDoc = oWord.Documents.Open(FileName:="C:calendar.doc")
    oDoc.Bookmarks("page").Range.Select

    but I don't know how I can retrieve all the text of the page.

    Does anybody know how to do it or give me some hints about how to accomplish this?

    Thanks a lot for your help
    Luis

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    The text of a document is stored in a number of StoryRanges. One of these contains most of the document, the others are for headers, footers, text boxes etc.

    If all you want is the text of the main story then <font color=blue>oDoc.Content.Text</font color=blue> will do.

    StuartR

  3. #3
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Hi Stuart,

    Thanks a lot for your answer.

    Yes, I need the text of the main story, but doing oDoc.Content.Text will retrieve the text of the whole document.
    What I need is to retrieve text but only from a specific page inside the document.
    Do you know if there is anyway to do this?

    Thanks again

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    > What I need is to retrieve text but only from a specific page inside the document.
    How do you plan to identify the specific page?

    <font color=blue>oDoc.Bookmarks("page").Range.Text</font color=blue> will give you all the text on the currently selected page, but you would first need to select something on the correct page.

    StuartR

  5. #5
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Thanks Stuart

    All right, I'll try that.

    Sorry but I am really newbie with this! [img]/forums/images/smilies/sad.gif[/img]

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Putting it all together might look like this:

    Dim aPage As Range
    aDoc.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
    aDoc.Bookmarks("page").Range.Select

    Set aPage = aDoc.Bookmarks("page").Range


    [edited for OLE]
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Kevin,

    Thanks four your answer.
    aDoc has not the Selection property. I have been trying to find it, I couldn't find any.

    I did this, but I don't know if it is the best way:

    For a = 1 To Pages

    MsgBox oDoc.Bookmarks("page").Range.Text

    Set oRange = oDoc.Bookmarks("page").Range
    oRange.Select
    oWord.Selection.MoveRight Unit:=wdCharacter, Count:=1


    Next a

    Unfortunately, I don't know how I can see the text of a SPECIFIC page.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Selection is a property of the Application object, not of a Document object. So Kevin's code should work if you replace "aDoc." from before Selection by "oWord." (without the quotes).
    To get the text on a specific page, replace Count:=2 by Count:=nn, where nn is the page number you want.
    Kevin's code returns a range object in aPage. You can get at the text on this page using the Text or FormattedText properties of this object.

  9. #9
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Thanks, that works great [img]/forums/images/smilies/smile.gif[/img]

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Sorry, that should have been


    aDoc.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
    aDoc.Bookmarks("page").Range.Select

    Line 1 goes to page two of the document
    Line 2 selects the entire page
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Retrieving text from a page in Word (VB/Word)

    Hans,
    Thanks for clarifying. I posted a correction too.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •