logo
search
Document Editing Problems

How to Use a Word Macro to Add Quotation Marks Without New Lines

Elise WilliamsElise Williams Oct 12, 2026 870 views

Question details

The user wants to enclose selected text in quotation marks using a VBA macro without having the closing quotation mark placed on a new line.

How to Use a Word Macro to Add Quotation Marks Without Moving the Closing Quote
Product
Microsoft Word
Device & OS
not provided
Scenario
Applying quotation marks around a block of text using a macro, especially when the selection includes trailing spaces or paragraph breaks.
Observed behavior
When the text selection includes a paragraph mark, inserting a closing quotation mark pushes it to a new line instead of keeping it at the end of the text block.
Before you start

Ensure that the Developer tab is enabled in your Word processor's ribbon so you can access the VBA Editor and create custom macros.

Solution 1Recommended

Use a VBA Macro to Trim the Selection Before Adding Quotes

This solution provides a custom VBA macro that automatically removes trailing paragraph marks and spaces from your selection before inserting the quotation marks.

By default, when you select an entire paragraph, Word includes the invisible paragraph mark at the end. The macro below intelligently moves the end of the selection backward, ignoring spaces, tabs, and line breaks, before applying the quotation marks.

1
Open the VBA Editor

Press the Alt + F11 keys on your keyboard to open the Microsoft Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the VBA Editor, click on 'Insert' in the top menu and select 'Module'. This will open a blank window where you can paste the macro code.

3
Paste the Macro Code

Copy and paste the following code into the module window: Sub AddQuotes() Dim sBegQ As String Dim sEndQ As String If Options.AutoFormatAsYouTypeReplaceQuotes Then sBegQ = Chr(147) sEndQ = Chr(148) Else sBegQ = Chr(34) sEndQ = Chr(34) End If Selection.MoveEndWhile CSet:=" " & Chr(160) & vbTab & vbCr & Chr(11), Count:=-1 Selection.InsertBefore sBegQ Selection.InsertAfter sEndQ End Sub

4
Run the Macro

Close the VBA Editor. Highlight the text in your document that you want to quote, then press Alt + F8, select 'AddQuotes' from the list, and click 'Run'.

Use a VBA Macro to Trim the Selection Before Adding Quotes
Smart Quotes Support: This macro automatically detects your AutoFormat settings. It will insert smart (curly) quotes if enabled, or standard straight quotes if disabled.

Automate Your Document Workflows with WPS Writer

WPS Office provides robust document editing features, including support for VBA macros in its advanced editions. You can easily apply custom formatting scripts, automate repetitive tasks, and edit documents seamlessly without compatibility issues.

  1. 1. Enable the Developer Tools: Open WPS Writer and navigate to the 'Developer' tab on the top ribbon menu to access the macro functionalities.
  2. 2. Open the Macro Editor: Click on the 'Macros' button or simply use the Alt + F8 keyboard shortcut to open the Macro management window.
  3. 3. Create and Paste Your Macro: Type a name for your macro (e.g., 'AddQuotes'), click 'Create' to open the VBA Editor, and paste your quotation mark code.
  4. 4. Execute the Code: Select the text block in your document, return to the Macros menu, select your newly created macro, and click 'Run'.
Excellent compatibility with Microsoft Office formats like .doc and .docx files.Supports advanced macro scripts to automate complex and repetitive document formatting tasks.Lightweight, fast, and features a familiar, easy-to-use tabbed interface with zero learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the closing quotation mark jump to the next line without using the macro?

When you select a paragraph in Word, the selection typically includes the invisible paragraph mark (line break) at the end. Inserting a closing quote places it after this paragraph mark, effectively pushing the quotation mark to a new line.

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

Yes. You can go to File > Options > Customize Ribbon, click on 'Customize' next to Keyboard shortcuts, find 'Macros' in the Categories list, select your 'AddQuotes' macro, and assign a new shortcut key combination for quick access.

Does this macro work smoothly with smart curly quotes?

Yes, the macro checks your 'AutoFormat As You Type' settings. If 'Replace straight quotes with smart quotes' is enabled in your preferences, the macro uses character codes 147 and 148 to correctly insert curly quotes.