I have tried to solve exactly the same problem without success. However, these are two different codes from two different projects that could be useful. Using Windows XP, Indesign CS, Visual Basic (not a script, but independent executable programs).
1. This code runs throughout the bookmarks of a document, obtains the number of the page of each one of them and prints it to a file (Print 1) . This part could be useful to obtain the page. Obviously this is different with just selected text, and that is the problem, but maybe it could help.
For thismarknum = 1 To mydoc(thisdocnum).Bookmarks.Count
Set thismark = mydoc(thisdocnum).Bookmarks.Item(thismarknum)
a = thismark.ShowBookmark
Set myframepage = thismark.Destination.DestinationText.ParentTextFrame.Parent
markname = thismark.Name: myframepagename = myframepage.Name
Print #1, "The Bookmark "; markname; " is in page"; myframepagename
marks = marks + 1
marksname(marks) = markname: markspage(marks) = myframepagename
Next 'thismarknum
2. This code is executes when pageminusbutton is clicked. Serves to go to the previous page and update a textbox in a VBform that indicates the current page.
Private Sub pageminusbutton_Click()
Set myindesign = CreateObject("InDesign.Application.CS")
Set mydoc = myindesign.ActiveDocument
Set currentpage = myindesign.ActiveWindow.ActivePage
numpag = currentpage.DocumentOffset
myindesign.ActiveWindow.ActivePage = mydoc.Pages.Item(numpag - 1)
Set currentpage = myindesign.ActiveWindow.ActivePage
namebox.Text = currentpage.Name
End
So far the code.
If I obtain new information about this issue I'll let you know. However, sometimes trying to change the approach to the probles serves, for instance, searching first page by page, then frame by frame inside the page, and then word by word inside the paragraph, in this mode you always know in which page/frame you are, instead of trying to obtain the parent.
Guillermo