logo
search
VBA & Macro Problems

How to Copy the Current Page Number to Clipboard with Word VBA

Muhammad TalhaMuhammad Talha Oct 21, 2026 868 views

Question details

The user wants a Word VBA solution to extract the current page number and save it to the Windows clipboard for pasting elsewhere.

How to Copy the Current Page Number to Clipboard with Word VBA
Product
Microsoft Word / WPS Writer
Device & OS
not provided
Scenario
Creating a manual document index where the user needs to quickly grab the page number of a selected heading and paste it manually, bypassing the standard Table of Contents (TOC) fields.
Observed behavior
The user requires a macro that retrieves the page number without disturbing the current cursor selection and successfully writes it to the system clipboard.
Before you start

Ensure that the Developer tab is enabled in your word processor and that your macro security settings allow the execution of VBA scripts.

Solution 1Recommended

Use Selection.Information and Windows API to Copy Page Number

Retrieve the current page number using the Selection.Information property and pass it to the clipboard using compatible Windows API declarations.

To avoid altering your document's text selection, you can directly query Word for the active end page number of your current cursor position.

Since standard VBA does not have a built-in direct-to-clipboard function without enabling external libraries, using a custom Clipboard_SetData procedure with Windows API declarations (compatible with both 32-bit and 64-bit Office) is the most robust method.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in your word processor.

2
Insert a New Module

Click on 'Insert' in the top menu, then select 'Module' to create a blank script window.

3
Declare the Windows Clipboard API

At the top of the module, paste the required Windows API declarations for clipboard access. Ensure you include 'PtrSafe' attributes to make the code compatible with 64-bit environments.

4
Create the Page Number Retrieval Macro

Write a subroutine that captures the page number. Use the line: pageText = CStr(Selection.Information(wdActiveEndPageNumber)).

5
Pass Data to the Clipboard Helper

Call your Clipboard_SetData helper procedure within the subroutine, passing the 'pageText' string so it gets copied to the Windows clipboard.

6
Run and Paste

Assign a keyboard shortcut to your new macro. Select a heading, press the shortcut, and use Ctrl + V anywhere else to paste the extracted page number.

Use Selection.Information and Windows API to Copy Page Number
Automation Tip: Once configured, this script completely automates fetching page numbers, making manual index creation significantly faster without messing up standard TOC fields.
Advanced VBA Support in WPS Office

Execute Complex VBA Macros Seamlessly in WPS Writer

WPS Office comes equipped with robust VBA (Visual Basic for Applications) support, allowing you to run document automation scripts natively. Creating custom macros to handle clipboard operations and page number extraction is straightforward with the WPS Developer tools.

  1. 1. Enable the Developer Tab: Open WPS Writer, go to 'Options', and enable the 'Developer' tools to access macro settings.
  2. 2. Launch the VBA Editor: Click the 'VBA Editor' button on the Developer ribbon (or press Alt + F11) to open the scripting environment.
  3. 3. Paste Your Script: Insert a new module and paste your Clipboard API declarations and page number retrieval code.
  4. 4. Assign a Quick Shortcut: Bind your new macro to a custom keyboard shortcut via WPS Writer's settings for blazing-fast manual indexing.
Full native support for VBA macro scripting and executionHigh compatibility with Microsoft Word macro formats (.docm)Lightweight architecture for faster document processing and script executionFree and intuitive Developer tab interface for seamless coding
microsoft office alternative - wps office

Frequently Asked Questions

Why use VBA instead of a built-in Table of Contents (TOC)?

Using a macro for manual indexing allows for absolute formatting control and prevents field-update conflicts. Some users find that standard TOC fields interfere with custom navigation macros.

Will running this macro affect my current text selection?

No. By using the Selection.Information property, the macro simply reads the page number data associated with the current cursor position without modifying or unselecting your highlighted text.

Why do I need to worry about 32-bit vs 64-bit API declarations?

Modern software often runs on 64-bit architectures. If you use older 32-bit Windows API code for the clipboard without the 'PtrSafe' keyword, the VBA compiler will throw a syntax error and the macro will not run.

Can I use the MSForms DataObject instead of Windows API?

Yes, if you enable the 'Microsoft Forms 2.0 Object Library' in your VBA references, you can use the DataObject to put text on the clipboard. However, the Windows API approach avoids requiring external library references.