Home >Software Tutorial >Office Software >How to get the page number of a Word document through VBA code
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.
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 ' 计算每一页的行数 linesCount = i - 1 MsgBox "每一页有 " & linesCount & " 行文字。" End SubThis 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(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.
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!