Selection object

K

ksr

hi all,

I am working on word automation using vc++.
I would like to know if there is a way to get/extract part of the text
from a selection.
For ex. if my selection returns the text, "This is an example" when I
execute the following code,

Field.Select() // where field is a hyperlink.
selectionObj = GetSelection();
selectionObj.GetText();

The above text selection spans across multiple pages. Assuming, the
text "This is" is on Page 1 and "an example" is on Page 2. How can I
read them as two different texts? Is there a way to move within
selection looking for a page break?

I appreciate your inputs.

Thankyou
 
J

Jean-Guy Marcil

ksr was telling us:
ksr nous racontait que :
hi all,

I am working on word automation using vc++.
I would like to know if there is a way to get/extract part of the text
from a selection.
For ex. if my selection returns the text, "This is an example" when I
execute the following code,

Field.Select() // where field is a hyperlink.
selectionObj = GetSelection();
selectionObj.GetText();

The above text selection spans across multiple pages. Assuming, the
text "This is" is on Page 1 and "an example" is on Page 2. How can I
read them as two different texts? Is there a way to move within
selection looking for a page break?

I appreciate your inputs.

Thankyou

Set a range to the selection, then compare the start point and end point of
the range using the Selection.Information(wdRelativePage...) to see if they
are on the same page. If so, you are done.

If not...
After selecting the start point of the original range, use
ActiveDocument.Bookmarks("\Page").range to create a range with the first
page of the selection.
Then create a range that will start with the original range and finish with
the page range end point. This will give you the range of the part of the
text at the bottom of the first page.
Repeat with the second page by selecting the end point of the original
range...

Looks complicated, but I am afraid that there is no way to find the actual
page break if the text just flows "naturally" from the bottom of a page to
the beginning of the next. In those case, there are no real page break.

There maybe an easier way, but that is all I can think of at this time of
the night!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

In general -- in any programming language -- the answer is no, you
can't "look for" the page break within the Selection. The reason is
that the page break isn't part of the text stream; it's dynamically
calculated by Word based on the margins, paragraph indents and
vertical spacing, the text content and size (as returned by the
current printer driver), the "keep with next"/"keep together"/"widow
and orphan" settings, and assorted other factors.

There are various (complicated) schemes you can try. One goes like
this:

- Declare a Word.Range object. Assign the Selection.Range to it to
"save" the location of the original selection so you can return to it.

- Collapse the Selection to its Start.

- Select the built-in bookmark Selection.Bookmarks("\page"). That
selects the entire page containing the start of the original
selection.

- Grab the value of Selection.End. That's where the current page break
is.

- Split the saved Range object into the piece before the page break
and the piece after the page break.

This is an inefficient algorithm, even in VBA (where you don't have to
worry about the slowness of cross-process OLE), because moving the
Selection object usually causes screen redrawing. Unfortunately, the
Bookmarks("\page") construct isn't available for Range objects.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
K

ksr

Thanks for your reply.
It sounds quite complicated. Can you give sample code to do it?
I am able to find out which page no. the text is on. I want to know how
to break and read the text based on page break from a selection.
I have one other question, I am able to change the end position of a
selection,
but I am not able to change the start position of the selection.
For ex.
s = GetSelection()
l = s.getstart()
l1 = s.getend()
s.setstart(l+10);
s.setend(l1+10);
The above code does not change the start pos.
pls. help.
 
K

ksr

Thankyou for your inputs.

In my code, I read and store all the fields (ie, hyperlinks ) within
the document. So at any time, I can call field.Select() to get the
selection.
As per your explanation, If I have code as below,

selection s = field.Select()

Range r = s.GetRange()
s.collapse(wdCollapseStart)

After this, I can call Selection.GetBookMarks() but it does not accept
any parameters. which method should I call to pass "\page"?

And how do I split a range object into two? Could you pls. explain.

Thanks.

I was thinking of another approach.

For ex. if I have a selection which starts on Page 1 and ends on Page
2.

- I get the original selection.
- I move the start position of selection by calling
Selection.MoveStart(wdCharacter, 1).
- I collapse the selection to start.
- Get the page# by Selection.Information(wdActiveEndPage..).
- Then, keep moving the start position, till I get to the next page#,
and then I stop.

I thought this approach ill reduce the selection by 1 character each
time I call MoveStart().
So if the original selection has the text, "This is an example",

MoveStart(),
s = GetSelection()
s.GetText();

will return, "his is an example" (without "T")

But this code does not seem to work.

Could you pls. tell if this approach is feasible? or any alternate ways
to do it?

Thanks.
 
J

Jean-Guy Marcil

ksr was telling us:
ksr nous racontait que :
Thankyou for your inputs.

In my code, I read and store all the fields (ie, hyperlinks ) within
the document. So at any time, I can call field.Select() to get the
selection.
As per your explanation, If I have code as below,

selection s = field.Select()

Range r = s.GetRange()
s.collapse(wdCollapseStart)

After this, I can call Selection.GetBookMarks() but it does not accept
any parameters. which method should I call to pass "\page"?

As you are writing in C++, I am afraid I cannot help you with that, this is
a VBA group after all ;-)
(GetBookMarks() is not part of the Word object library)
And how do I split a range object into two? Could you pls. explain.

Declare two range variables (Range1 and Range2)

Set Range1 to the whole original range.
Set Range2 to start where Range1 starts and to finish somewhere in the
middle.
You now have two ranges where Range2 is a sub-range of Range1.

Thanks.

I was thinking of another approach.

For ex. if I have a selection which starts on Page 1 and ends on Page
2.

- I get the original selection.
- I move the start position of selection by calling
Selection.MoveStart(wdCharacter, 1).
- I collapse the selection to start.
- Get the page# by Selection.Information(wdActiveEndPage..).
- Then, keep moving the start position, till I get to the next page#,
and then I stop.

I thought this approach ill reduce the selection by 1 character each
time I call MoveStart().
So if the original selection has the text, "This is an example",

MoveStart(),
s = GetSelection()
s.GetText();

will return, "his is an example" (without "T")

But this code does not seem to work.

Could you pls. tell if this approach is feasible? or any alternate
ways to do it?

I would store the Selection end point.
Collapse the selection to its beginning.
Move the end point until it is either on a different page or until you reach
the stored end point, whichever come first.
If the End point comes first, the whole selection is on the same page, if
not....
Set a range to this new selection.
Collapse to the end ( you should now be at the beginning of the next page)
Set a range to start form the current selection point to the previously
stored end point.

Again, because you are dealing with C++, you are going to figure out the
code by yourself!

Good luck.



--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

ksr

Thanks for your inputs.

I understood upto the point where I try to move the endpoint till I
reach a different page or the stored end point. But after that,
"Set a range to this new selection.
Collapse to the end ( you should now be at the beginning of the next
page)
Set a range to start form the current selection point to the
previously
stored end point."

"Set a range to this new selection.
- Are you saying, I should create a range, with the stored start point
and the new end point now.

"Collapse to the end ( you should now be at the beginning of the next
page) "
- Is it that since collapse end refers to the range is located after
ending paragraph point, it will now be at start of next page?

After this, how to set a range to start from current selection point to
previously stored end point.

Can you pls. give me some code snippets in VBA? I will try to find
equivalent c++ code.

Thankyou very much.
 
J

Jean-Guy Marcil

ksr was telling us:
ksr nous racontait que :
Thanks for your inputs.

I understood upto the point where I try to move the endpoint till I
reach a different page or the stored end point. But after that,
"Set a range to this new selection.
Collapse to the end ( you should now be at the beginning of the next
page)
Set a range to start form the current selection point to the
previously
stored end point."

"Set a range to this new selection.
- Are you saying, I should create a range, with the stored start point
and the new end point now.

"Collapse to the end ( you should now be at the beginning of the next
page) "
- Is it that since collapse end refers to the range is located after
ending paragraph point, it will now be at start of next page?

After this, how to set a range to start from current selection point
to previously stored end point.

Can you pls. give me some code snippets in VBA? I will try to find
equivalent c++ code.

Try this in VBA:

Dim FullRange As Range
Dim RangePart1 As Range
Dim RangePart2 As Range
Dim FullRangeEndPoint As Long
Dim WeHaveTwoRanges As Boolean


WeHaveTwoRanges = False

'Set FullRange = Selection.Fields(1).Result
Set FullRange = Selection.Paragraphs(1).Range

FullRangeEndPoint = FullRange.End
Set RangePart1 = FullRange

With RangePart1
'Start and end not on same page then below is true
If .Characters(1).Information(wdActiveEndPageNumber) <> _
.Characters(.Characters.Count).Information(wdActiveEndPageNumber)
Then
.Collapse wdCollapseStart
.MoveEnd wdCharacter, 1
WeHaveTwoRanges = True
'true if next charcter on same page as last character in current
range
Do While .Next.Characters(1).Information(wdActiveEndPageNumber) = _
.Characters(.Characters.Count).Information(wdActiveEndPageNumber)
.MoveEnd wdCharacter, 1
Loop
Set RangePart2 = ActiveDocument.Range(RangePart1.End, _
FullRangeEndPoint)
End If
End With

If WeHaveTwoRanges Then
'use RangePart1 and RangePart2
RangePart1.Select
RangePart2.Select
Else
'Use FullRange
FullRange.Select
End If


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

ksr

Thanks for taking time to write the whole code.
I will try to find equivalent c++ code and will try to implement this.
hope it works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top