Home >Software Tutorial >Office Software >How to get the page number of a Word document through VBA code

How to get the page number of a Word document through VBA code

WBOY
WBOYforward
2024-01-23 15:18:241632browse

1. VBA code to obtain the page number of the Word document

To obtain the page number of the Word document through the VBA code, you can use the ActiveDocument.ComputeStatistics method, as follows Display:

Sub GetPageCount()
    Dim pageCount As Integer
    ' 计算文档页数
    pageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)
    MsgBox "文档共 " & pageCount & " 页。"
End Sub

This code calculates the number of pages in the Word document through the wdStatisticPages parameter, and displays the page number information through the MsgBox pop-up window.

2. In Word, if you have a question about how many pages there are in total after sectioning, you can use the following steps. Solution:

1. Insert a section break (for example, a breakpoint symbol) where a section needs to be broken.

    2. Insert page numbers in the footer of each section and select "Format Page Numbers".
  1. 3. In the "Page Number Format" dialog box, select "Continue Previous Section" or "Start from Previous Section" to set the starting position of the page number.
  2. In this way, the page number of each section will be counted independently, thus solving the problem of how many pages there are in total after sectioning.

3. Use VBA in Word to find out how many lines of text are on each page

To use VBA in Word to find out how many lines of text are on each page, You can use the following code:

Sub GetLinesPerPage()
    Dim i As Integer
    Dim linesCount As Integer
    Dim currentPage As Integer

    ' 获取当前页数
    currentPage = Selection.Information(wdActiveEndAdjustedPageNumber)

    ' 移动到文档末尾
    Selection.EndKey Unit:=wdStory

    ' 循环向上查找直到页数变化
    Do
        Selection.MoveUp Unit:=wdLine
        i = i + 1
    Loop Until Selection.Information(wdActiveEndAdjustedPageNumber) <> currentPage

    &#39; 计算每一页的行数
    linesCount = i - 1
    MsgBox "每一页有 " & linesCount & " 行文字。"
End Sub

This code first gets the current page number, and then calculates the number of rows on each page by moving the cursor up until the page number changes, and passes

MsgBox

A pop-up window displays the results.

Summary

(1) Use the

ActiveDocument.ComputeStatistics
    method to obtain the number of pages in a Word document through VBA code.
  1. (2) After sectioning in Word, the problem of how many pages there are in the page number can be solved by inserting a section break at the section break and setting the starting position of the page number.
  2. (3) Use VBA in Word to calculate how many lines of text there are on each page by moving the cursor until the number of pages changes.

The above is the detailed content of How to get the page number of a Word document through VBA code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete