Search code examples
headerms-wordbookmarksvisual-foxprooffice-2003

Find out the text between two bookmarks in the page header MSWord


I'm looking for a way to find out the text content between two bookmarks placed on the MSWord page header.

Suppose two bookmarks are BKM_1 and BKM_2. also the text between two booksmarks is "Test".

Local loWordApplication, loDoc, loRange
loWordApplication = CREATEOBJECT("Word.Application")
loDoc = loWordApplication.Documents.Open("C:\Example.doc")
IF loDoc.BookMarks.EXISTS("BKM_1") AND loDoc.BookMarks.EXISTS("BKM_2") 
   loRange = loDoc.RANGE(loDoc.BookMarks("BKM_1").START, loDoc.BookMarks("BKM_2").START) 
   loRange.SELECT
   ?loRange.TEXT && Printing the value here and expecting the result as "Test"
ENDIF

But eventhgough the bookmarks exist, above doesn't produce the result that I'm looking for. Please help!


Solution

  • Headers, footers are a different type of story < g > - no pun intended:

    #Define wdCharacter 1
    Local loWordApplication, loDoc, loRange
    loWordApplication = Createobject("Word.Application")
    loDoc = loWordApplication.Documents.Open("C:\Example.doc")
    If loDoc.BookMarks.Exists("BKM_1") And loDoc.BookMarks.Exists("BKM_2") And ;
            loDoc.BookMarks("BKM_1").StoryType = loDoc.BookMarks("BKM_2").StoryType
        Local lnCharacters
        lnCharacters = loDoc.BookMarks("BKM_2").Start-loDoc.BookMarks("BKM_1").Start
        If loDoc.BookMarks("BKM_1").StoryType != 1
            loDoc.StoryRanges(loDoc.BookMarks("BKM_1").StoryType).Select()
        Endif
    
        loDoc.BookMarks("BKM_1").Range.Select()
        loDoc.Application.Selection.Moveend(wdCharacter, m.lnCharacters)
    
        loRange = loWordApplication.Selection
    
        ?loRange.Text && Printing the value here and expecting the result as "Test"
    Endif
    loWordApplication.Quit