Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I detect an 'End of Page' in a new Word Doc?? 1

Status
Not open for further replies.

vbLew

Programmer
Feb 28, 2005
22
US
Hello All:

Just wanted to know if there is some vba code out there that someone might know about that will detect when my Word Selection is about to move to a new page.

I'm writing code to generate an outline, and I want to be able to mark the bottom of each page -as the text is generated- with a specific number before I go on to the next page. The problem is I am unable to detect if I am at the end of page or not.....

Is there a 'max num of line' property for a Word Doc Page???

Thanks!!
 
This is kind of messy. Could you please explain
I'm writing code to generate an outline, and I want to be able to mark the bottom of each page -as the text is generated- with a specific number before I go on to the next page.

You want to generate an outline? What does that mean? An outline is a structure.

Mark the bottom of each page...with what?

Perhaps with more details? Some code?

Gerry
 
Ignore the outline stuff,
All I want to know is, with VBA, can I detect the an 'end of a new page' before I go on to the next. Or Even better, Can I tell what current page my selection is on?
 
I detect the an 'end of a new page' before I go on to the next.

If it is a NEW page, then there is no "next". Plus, Word calculates pages. If you have one word on a page, then page end is at the end of that word. If you have 100 words on a page, then end of page is at the end of those words.

Or Even better, Can I tell what current page my selection is on?

What do you mean? Do you mean what page number it is? The page the selection is on IS the current page.

Please clarify exactly what it is you are trying to do.

Gerry
 
Nevermind Gerry,
Thanks anyways...
I wanted to create code to do something like this:

psdedo code:

string array(3) MyArray = GoodMorning,GoodAfternoon,Goodevening

For i =1 to 3
Do While MySelection is Not OnANewPage()
Selection.TypeText array(i)
If MySelection is OnANewPage()
Selection.TypeText "Page" & i
end if
End Do

next for

Anyways, Thanks for trying.


 
Here's the solution that I can up with however;
This function only tells me after I am on a new page and not right before, which is what I want.




' member var
Private mCurrentPage as long

'
Private Sub SetCurrentPage()
mCurrentPage = mWdDoc.Content.Information(wdActiveEndAdjustedPageNumber)
End Sub

' have to SetCurrentPage once before using this
Private Function NewPage() As Boolean

If Not mCurrentPage= mWdDoc.Content.Information (wdActiveEndAdjustedPageNumber) Then

mCurrentPage = mWdDoc.Content.Information(wdActiveEndAdjustedPageNumber)

NewPage = True
Else
NewPage = False
End If

End Function


 
Hi vbLew,

What it seems you are trying to do is a bit of word processing, but that is what Word does for you, so why not let Word do what it does and you can get on with doing what you do?

What I think you want is a Page Footer and/or Page Header.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks for the appreciation...but I think it is not warranted, as I did not do anything. Frankly, it sounds more like sarcasm, but what the heck.

This function only tells me after I am on a new page and not right before, which is what I want.

I will repeat: if you are on NEW page, there is no next page. How could there be? That is why it only tells you when you are on a new page "not before". There IS NO before, because there IS NO next page. As far as Word is concerned the document ends there (and it does), so there is NO next page. Word can not read minds; it can not tell if you are going to put in more text that will need a next page.

Also, .Content.Information(wdActiveEndAdjustedPageNumber) returns the page number of the last page.

Say you have a 13 page document. Your sub SetCurrentPage will make mCurrentPage = 13...REGARDLESS of where the Selection is. And, as I stated previously, current page is (normally) defined as where the Selection is. So if the Selection is on page 4, your mCurrentPage will = 13. hardly the current page now is it?

Further, your logic is faulty in the function. Continuing with the 13 page document example.

if mCurrentPage is NOT = 13, then
mCurrentPage = 13
NewPage = True
Else
NewPage = False
end if

That would be fine...except you have a comment to run SetCurrentPage first - which will make mCurrentPage = 13.

Please relax, don't thank me for something I have not helped with, and kindly state what it is you are trying to do.

"New" page. "Next" page.

Are you talking about a new page INSERTED into a document, then yes, sure, there is a next page.

But if it a word processing function (i.e. making another page, a "new" page) then there is NO "next" page.

If what you want is some routine that WHEN there is a new page created, that is another story, but you have not asked for that.

You asked for:
All I want to know is, with VBA, can I detect the an 'end of a new page' before I go on to the next. Or Even better, Can I tell what current page my selection is on?

The first part...no you can not, because there is no "next" page, unless you are on a page that already has pages after it.

The second part. Yes you can.

I may also point out that your code completely ignores the location of the Selection, which - although you did not state what it is you are trying to do - may be something you are trying to work with.

Gerry
 
Here is a Sub that may, I hope, help a little bit. I use this (or a variation) whenever I need to know where the selection is, in relation to various other parts of the document.

It makes four Range objects:
From start of current page to selection
From start of current page to end of current page
From start of document to selection
From selection to end of document

From these you can pinpoint a number of locations relative to the selection point. Also, you can extract various calculations of paragraph counts, line counts, page counts...

NOTE: this is the basic sub, and assumes the selection point is collapsed. If you run this with an extended selection the numbers returned are for Selection.Range.Start

Code:
Sub WhereAmI()
Dim aDoc As Document
Dim CurPageRange As Range
Dim DocStartToSelection As Range
Dim PageStartToSelection As Range
Dim SelectionToDocEnd As Range
Dim VerticalMessage As String
Dim msg As String
Dim title As String
Dim response

title = "Selection point information"
Set aDoc = ActiveDocument

Set PageStartToSelection = _
    aDoc.Range(Start:=aDoc.Bookmarks("\page").Range.Start, _
    End:=Selection.Range.Start)
    
Set CurPageRange = _
    aDoc.Range(Start:=aDoc.Bookmarks("\page").Range.Start, _
    End:=aDoc.Bookmarks("\page").Range.End)
   
Set DocStartToSelection = _
    aDoc.Range(Start:=0, End:=Selection.Range.Start)
    
Set SelectionToDocEnd = _
    aDoc.Range(Start:=Selection.Range.End, _
    End:=aDoc.Bookmarks("\endofdoc").Range.End)

If Selection.Range.Information(wdVerticalPositionRelativeToPage) = _
        aDoc.PageSetup.TopMargin Then
    VerticalMessage = "Selection is on first line of page."
Else
    VerticalMessage = "Selection is on line " & _
        PageStartToSelection.ComputeStatistics(wdStatisticLines) + 1
End If

msg = "Page Number: " & Selection.Range.Information(wdActiveEndPageNumber) & _
    vbCrLf & "Pages to Document End: " & (aDoc.Content.Information(wdActiveEndPageNumber) - Selection.Range.Information(wdActiveEndPageNumber)) & _
    vbCrLf & _
    vbCrLf & "Absolute Character Number: " & Selection.Range.Start & _
    vbCrLf & _
    vbCrLf & "Page Paragraph Number: " & PageStartToSelection.Paragraphs.Count & _
    vbCrLf & "Document Paragraph Number:" & DocStartToSelection.Paragraphs.Count & _
    vbCrLf & "Paragraphs To Document End: " & SelectionToDocEnd.Paragraphs.Count & _
    vbCrLf & _
    vbCrLf & "Vertical Distance on Page: " & VerticalMessage

Set CurPageRange = Nothing
Set DocStartToSelection = Nothing
Set PageStartToSelection = Nothing
Set SelectionToDocEnd = Nothing
Set aDoc = Nothing
response = MsgBox(msg, vbOKOnly, title)
End Sub

Gerry
 
If you are trying to keep page breaks in comfortable locations, the best way to do that is use paragraph styles with some combination of widow and orphan control, keep lines together, and keep with next (see the format paragraph choices).

If you assign these to paragraph styles based on your usage of the paragraphs in the document, you should be able to achieve much of what you want without VBA.
 
Good point. Bang on. It emphasizes again that the only real way to use Word properly is to use Styles.

Gerry
 
I appreciate all the feed back.

Just to let you know I tried using the footer in Word to solve the customer's problem originally; however, the text in the footer changed so much that it got to be a pain, trying to break the link from the previous footer in one section and not in another.

Eventually, I came up with this function below and it work fine for what I need.

Private Function EndOfPage() As Boolean
Dim LengthOfPage as Long
LengthOfPage = 9.5
EndOfPage = PointsToInches(Selection.Information(wdVerticalPositionRelativeToPage)) > LengthOfPage
End Function


 
Your function will work, as you hard coded a number. If it works out for you...great.

You stae you tried footers. Footers can work extremely well, but they do require a biot of understanding of how the Word object model is set up. Andyes, the link to previous can mess things up. That is why I also explicitly design footers, and just about ALWAYS unlink headers and/or footers from previous.

I find it interesting that in your last post, you wrote:
to solve the customer's problem originally

May I point out, that this is the first mention of "customer's problem". When posting here, it is very helpful to actually state what the problem is (it makes no difference if it is your problem, or a client's problem). Quite often, surprsingly often, when thwe problem is clearly stated, the posted question is redirected to s proper solution. This is because very often, and I do it myself, we ask questions that are NOT truly connected to the problem. When the problem is stated, it makes things a LOT easier to think of solution.

You asked about knowing when the next page is coming. You still have not stated what the problem - and I am still curious. That you found something that works is great, and your function is duly noted. However, if you don't mind, could you share with us, what the problem was. Not your question - I gwt that, but what was the problem, that your function builds a solution for.

If you don't mind sharing. Perhaps we can learn something.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top