Document Editing Problems

How to Use a Word VBA Macro to Paste Floating Shapes at the Cursor

Nimra MalikNimra Malik Nov 3, 2026 872 views

Question details

The user needs a VBA macro solution to precisely paste floating shapes or images exactly at the current cursor location to prevent them from shifting away from their anchors.

How to Use a Word VBA Macro to Paste Floating Shapes at the Cursor
Product
Microsoft Word
Device & OS
not provided
Scenario
Pasting floating images or shapes into a document where strict positioning relative to the text cursor (character and line) is required.
Observed behavior
When pasting floating objects in Word, they frequently appear far from their text anchors or even outside the visible page area, requiring manual realignment.
Before you start

Ensure that the Developer tab is enabled in your Word ribbon to access the VBA editor, and make sure you have already copied a shape or image to your clipboard.

Solution 1Recommended

Create and Run a Custom VBA Macro

Use a VBA script to automatically paste the clipboard contents, target the shape range, and set its horizontal and vertical alignment directly to the cursor's character and line.

This macro utilizes the Selection.Paste method and immediately adjusts the Selection.ShapeRange object. By setting the RelativeHorizontalPosition to the character and the RelativeVerticalPosition to the line, the shape is forced to stay exactly where the cursor was placed.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard while Microsoft Word is open to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu bar and select 'Module' to create a new blank script window.

3
Paste the Macro Code

Type or paste the VBA code into the module. Start with 'Sub FloatingPasteAndSetPosition()', then use 'Selection.Paste'. Add a With block for 'Selection.ShapeRange' setting '.RelativeHorizontalPosition = wdRelativeHorizontalPositionCharacter', '.RelativeVerticalPosition = wdRelativeVerticalPositionLine', '.Left = InchesToPoints(-0.2)', and '.LockAnchor = True'.

4
Run the Macro

Place your cursor at the exact text location in your document where you want the shape. Ensure a shape is copied to your clipboard, press 'Alt + F8', select 'FloatingPasteAndSetPosition', and click 'Run'.

Error Handling for Non-Shapes: It is highly recommended to include 'On Error GoTo NotShape' right after the paste command to display a MsgBox if the user accidentally attempts to paste text or an inline object instead of a floating shape.
Free Microsoft Office alternative

Easily Manage Document Layouts and Shapes with WPS Office

While complex VBA macros are sometimes required in Word for precise object placement, WPS Writer offers highly intuitive layout tools for floating shapes without the need for custom coding. Experience a lightweight, free alternative that makes managing document graphics incredibly simple.

Fully compatible with Microsoft Word (.docx) formats, ensuring your shapes and anchors render correctly.Intuitive picture and shape layout options directly accessible from the right-click menu or side panel.Built-in advanced object positioning tools for exact horizontal and vertical placement without VBA.Lightweight installation and smooth performance even with media-heavy documents.
microsoft office alternative - wps office

Frequently Asked Questions

Why do my pasted images in Word jump to the wrong page?

By default, Word may insert images inline or anchor floating images to the nearest paragraph. If the paragraph text shifts, or if the relative positioning is set to the page margin instead of the specific text line, the image will appear detached from the location where your cursor originally was.

How do I manually lock an image anchor without using VBA?

Select the inserted image, click the 'Layout Options' icon floating next to it, choose 'See more...', navigate to the 'Position' tab, and check the 'Lock anchor' box at the bottom of the dialog window.

Can I assign a keyboard shortcut to this VBA macro for faster pasting?

Yes. Go to 'File' > 'Options' > 'Customize Ribbon', and click the 'Customize' button next to Keyboard shortcuts. Select 'Macros' in the Categories list, choose your custom floating paste macro, and assign a preferred key combination like 'Ctrl + Shift + V'.

What does 'wdRelativeHorizontalPositionCharacter' do in the code?

It is a VBA enumeration that tells the word processor to calculate the shape's horizontal offset starting exactly from the specific text character where the anchor is placed, rather than measuring from the column, margin, or page edge.