Microsoft KB Archive/285599

= How to find the end of a page in a Word document by using Automation =

Article ID: 285599

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Word 2000 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q285599



SUMMARY
This article describes automatic methods for finding the end of a page in a Microsoft Word document.



MORE INFORMATION
There are two methods that you can use to find the end of a page:
 * You can use the pre-defined bookmark &quot;\page&quot;. -or-


 * You can use Word's Browser object to change the insertion point to the browser target wdBrowsePage.

The following samples make use of a test document that spans multiple pages. In the test document, a page may end with a paragraph mark, or a page may wrap to a new page, splitting a paragraph between the two pages. Before starting the steps to build the samples below, create a three-page document in Microsoft Word and save it as C:\ThreePageDocument.doc.

Sample #1: Using a Predefined Bookmark
 Start a new Visual Basic Standard EXE project. Form1 is created by default. Add three command buttons to Form1. Change the captions for Command1, Command2, and Command3 to Use \Page Bookmark Method, Move to Next \Page Bookmark and Quit , respectively. From the Project menu, click References. Select Microsoft Word 9.0 Object Library, and then click OK. Note For Word 2002, select the Microsoft Word 2002 Object Library. For Microsoft Office Word 2003, select the Microsoft Word 11.0 Object Library.

  Copy the following code into the code module of Form1: Option Explicit Dim oWord As Word.Application Dim oDoc As Word.Document Dim oRange As Word.Range Dim iPageNumber As Integer Dim iCount As Integer

Private Sub Command1_Click Set oWord = New Word.Application oWord.Visible = True iPageNumber = 1    ' Initialize Set oDoc = oWord.Documents.Open(FileName:=&quot;C:\ThreePagedocument.doc&quot;) oDoc.Bookmarks(&quot;\page&quot;).Range.Select    ' \page is internal to Word oWord.Selection.Collapse direction:=wdCollapseEnd    ' Position insertion point oWord.Selection.MoveLeft Unit:=wdCharacter, Count:=1 Set oRange = oWord.Selection.Range oRange.Text = &quot;*&quot; oRange.Collapse direction:=wdCollapseEnd oRange.Select oWord.Selection.MoveRight Unit:=wdCharacter, Count:=1 MsgBox &quot;Click OK to close this box, then click the document to see the insertion point&quot;, vbMsgBoxSetForeground End Sub

Private Sub Command2_Click iPageNumber = iPageNumber + 1 ' Point to next page iCount = 2 oDoc.Bookmarks(&quot;\page&quot;).Range.Select ' Word's internal bookmark oWord.Selection.Collapse direction:=wdCollapseEnd ' move insertion point If Asc(oWord.Selection.Characters(1).Text) = 13 Then ' Is this a paragraph mark? ' Assume you're at last paragraph of the last page Set oRange = oWord.Selection.Range oRange.Text = &quot;*&quot; ' Show that you're at the end of the page Else ' Not at the end of last paragraph on last page oWord.Selection.MoveLeft Unit:=wdCharacter, Count:=2 'Possible Para and Page symbols ' Check for Autowrap from one page to the next. If Asc(oWord.Selection.Characters(1).Text) <> 13 Then 'it is not a paragraph oWord.Selection.MoveUp Unit:=wdLine, Count:=1 ' was autowrapped, so force ' current line to next page ' if inserted text is to go                                                         ' on this page End If       Set oRange = oWord.Selection.Range oRange.Text = &quot;*&quot; ' Show that you're at the end of the page oRange.Collapse direction:=wdCollapseEnd oRange.Select oWord.Selection.MoveRight Unit:=wdCharacter, Count:=2 ' past the para and asterisk End If   MsgBox &quot;Dismiss this message, then click the document to see the insertion point.&quot;, vbMsgBoxSetForeground End Sub

Private Sub Command3_Click If Not (oDoc Is Nothing) Then oDoc.Saved = True oDoc.Close Set oDoc = Nothing oWord.Quit (False) Set oWord = Nothing End If   Unload Me End Sub  Press F5 to start the program. Click Use \Page Bookmark Method. First, Word starts and opens your ThreePageDocument.doc file. Then a message box appears. Follow the instructions that appear in that box. For subsequent pages, click Move to Next \Page Bookmark.</li> To quit Word and terminate the demonstration, click Quit.</li></ol>

Sample #2: Using the Browser Object
The code in this section demonstrates that you can separate the content one page at a time from a multi-page document and copy that content into a new document. <ol> Start a new Visual Basic Standard EXE project. Form1 is added by default.</li> Add two command buttons to Form1. Change the caption of Command1 to Use Word Browser Method and the caption of Command2 to Quit .</li> From the Project menu, click References. Select Microsoft Word 9.0 Object Library, and then click OK.NOTE: For Word 2002, select Microsoft Word 2002 Object Library. For Microsoft Office Word 2003, select the Microsoft Word 11.0 Object Library.

</li>  Copy the following code into the code module of Form1: Option Explicit Dim oWord As Word.Application Dim oDoc As Word.Document Dim oNewDoc As Word.Document Dim oRange As Word.Range Dim iPageNumber As Integer Dim iCount As Integer Dim strTestDir As String Dim strTestFile As String

Private Sub Command1_Click Command1.Visible = False Dim lCurrentStart As Long Dim lCurrentEnd As Long Dim lDocumentEnd As Long Dim lOutputCount As Long lOutputCount = 0 'Launch Word and make it visible Set oWord = CreateObject(&quot;Word.Application&quot;) oWord.Visible = True 'Open the test document Set oDoc = oWord.Documents.Open(FileName:=&quot;C:\ThreePageDocument.doc&quot;) 'Find the beginning end of the document oDoc.Select lCurrentStart = oWord.Selection.Start lCurrentEnd = lCurrentStart lDocumentEnd = oWord.Selection.End 'Move the insertion point to the beginning of the document oWord.Selection.Collapse wdCollapseStart Do While (lCurrentEnd < lDocumentEnd) 'Move the insertion pointer to the bottom of this page oWord.Browser.Target = wdBrowsePage oWord.Browser.Next lCurrentEnd = oWord.Selection.End 'On the last page, the start and end will be the same If (lCurrentStart = lCurrentEnd) Then lCurrentEnd = lDocumentEnd End If       'Capture the Range of the current page Set oRange = oDoc.Range(lCurrentStart, lCurrentEnd) 'Create a new document and copy the range to it       Set oNewDoc = oWord.Documents.Add oRange.Copy oNewDoc.Range(0, 0).Paste 'Release the Range so we don't leak references Set oRange = Nothing 'Save the new document and close it       oNewDoc.SaveAs FileName:=&quot;C:\Result&quot; & lOutputCount & &quot;.doc&quot; ' You can save as another FileFormat. If so, change the ' file extension accordingly. oNewDoc.Close Set oNewDoc = Nothing 'Increment the output counter so we don't overwrite this file later lOutputCount = lOutputCount + 1 'Reset the current start position lCurrentStart = oWord.Selection.End Loop End Sub

Private Sub Command2_Click If Not (oDoc Is Nothing) Then oDoc.Saved = True oDoc.Close Set oDoc = Nothing oWord.Quit (False) Set oWord = Nothing End If   Unload Me End Sub </li> Press F5 to start the program.</li> Click Use Word Browser Method.</li> The code will generate a new Word document for each page in the document. Examine the following documents:

C:\Result0.doc

C:\Result1.doc

C:\Result2.doc

</li></ol>

(c) Microsoft Corporation 2001, All Rights Reserved. Contributions by Chris Jensen, Microsoft Corporation.

<div class="references_section">