How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

This article will demonstrate you how to remove the formatting of pasted text with macro and VBA in your word.

It happens a lot when you copy a text online, and you find the text is in rich formatting. For instance, there are repeating words, blank lines, tab characters, etc. Yet, such formatting can be quite annoying especially at times you don’t need. Naturally, you would remove the formatting manually each by each. However, what if the text is a really long one? Then the above method will be less efficient.

As a matter of fact, there is a way to utilize macro to strip all the text formatting once for all. Here is more:

Steps to Record a Macro in Word

  1. Firstly, open your Word.
  2. And make sure you have added “Developer” tab on the Ribbon first. For detailed instructions to add “Developer”, please read the article: How to Insert Background Music into Your Word Document
  3. Once “Developer” tab is available, click it.
  4. Then click “Record Macro” in “Code” group.Click "Developer" -> Click "Record Macro"
  5. Now a “Record Macro” dialog box will pop up. First, type the macro name, such as “copytext” here.
  6. Then in “Assign macro to” part, choose either “Button” or “Keyboard”. Here we will go “Button”.
  7. Next, click “OK”.Name the Macro -> Choose Either "Button" or "Keyboard" for "Assign macro to" -> Click "OK"
  8. And now you will see the “Word Options” window. Click “Customize Ribbon”.
  9. Then find a location for the macro on the right side. Here we will click “New Group” to set a new one for the macro.
  10. Noe continue to choose “Macros” from the drop-down list of “Choose commands from”.
  11. Next click the macro you just create.
  12. Then click “Add” in the middle of the window.
  13. Now the macro will be placed in the new group. Just right click the macro.
  14. And choose “Rename” option.Click "Customize Ribbon" -> Choose a Place for the Macro -> Choose "Macros" in "Choose commands from" -> Click the Macro -> click "Add" -> Right Click the Macro -> Choose "Rename"
  15. In “Rename” dialog box, choose a symbol for your macro.
  16. Then type the display name.
  17. Click “OK”.Choose a Symbol -> Type Display Name -> Click "OK"
  18. Now go back to “Word Options” window, and click “OK” there. The macro starts recording from now.Click "OK" in "Word Options"Word Starting Recording a Macro

Paste Text as Unformatted Text

  1. Firstly, copy a part of a long text.
  2. Then click “Home” tab.
  3. Click “Paste”.
  4. Next, choose “Paste Special”.Click "Home" -> Click "Paste" -> Click "Paste Special"
  5. And choose “Unformatted Text” in “Paste Special” dialog box.
  6. Then click “OK”. You may get a text with formatting such as below:Choose "Unformatted Text" -> Click "OK"Text Pasted as Unformatted Text

Remove Repeating Words

  1. Next start clearing the formatting left. Press “Ctrl+ H” to open the “Find and Replace” dialog box.
  2. And type “Arrow” in “Find what” text box.
  3. Then click “Replace All”.Type the Repeating Word in "Find what" Text Box -> Click "Replace All"

Remove Tab Characters

  1. Firstly, press “Ctrl+ H” to open the “Find and Replace” dialog box.
  2. Then type “^t” in “Find what” text box.
  3. Next click “Replace All” to remove all tab characters.Type "^t" in "Find what" Text Box -> Click "Replace All"

Remove Extra Blanks

  1. Still press “Ctrl+ H” to open the “Find and Replace” dialog box.
  2. In “Find what” text box, press the blank key twice.
  3. Then click “Replace All” button.Press Blank Key Twice in "Find what" Text Box -> Click "Replace All"

Remove Blank Lines and Extra Blanks Together

Sometimes, when using “Find and Replace” function to remove the formatting, you have to adjust a bit to meet your own need. For example, if you fail to remove all extra blanks when applying the above steps, you can use the steps below to delete them together with blank lines.

  1. Similarly, press “Ctrl+ H” to open the “Find and Replace” dialog box.
  2. Next in “Find what” text box, press a blank key first.
  3. Then type “^p”.
  4. In “Replace with” text box, type “^p”.
  5. Click “Replace All”.Press Blank Key First -> Type "^p" in "Find what" Text Box -> Type "^p" in "Replace with" Text Box -> Click "Replace All"
  6. Again open the “Find and Replace” dialog box.
  7. And in “Find what” text box, first type “^p”.
  8. Then press blank key once.
  9. Then type “^p” in “Replace with” text box.
  10. Finally click “Replace All” button.Type "^p" First -> Press Blank Key Once in "Find what" Text Box -> Type "^p" in "Replace with" Text Box -> Click "Replace All"

Remove the Left Blank Lines If Necessary

Still, if there are blank lines left, try following steps:

  1. Press “Ctrl+ H” to open the “Find and Replace” dialog box.
  2. next type “^p^p” in “Find what” text box.
  3. And type “^p” in “Replace with” text box.
  4. Finally, click “Replace All” button.Type "^p^p" in "Find what" Text Box -> Type "^p"in "Replace with" Text Box -> Click "Replace All"

By now, you may get result as below:Effect of Using "Replace" Function to Remove Formatting

We here suggest you take the above order to remove the text formatting so you can generally erase the formatting. And like we’ve mentioned, sometimes you have to clear other kinds of formatting, then you should use the “Find and Replace” function on your own. For more ways to remove formatting, you can visit this article: 3 Ways to Remove Unwanted Elements from Your Word Documents Instantly

You can check the following video to see the demonstration:

Stop Recording Macro

  1. Once you finish removing the text formatting, click “Developer” tab.
  2. Then click “Stop Recording”. Now the macro will be saved.Click "Developer" -> Click "Stop Recording"

How to Run a Macro

  1. Copy the text with formatting online, say.
  2. Open Word next.
  3. Then click “Developer” tab.
  4. Click the macro icon and you will see the text on the document without additional formatting.Click "Developer" -> Click the Macro Just RecordedEffect of Using Macro to Remove Formatting

Note: For the novice, you can just take the above steps to create and run a macro. Running a macro will help you get rid of the irritating formatting at large, which is enough.

The VBA Codes for the Expert

First there is the way to view the VBA codes.

  1. Firstly click “Developer” tab.
  2. Then click “Visual Basic’ in “Code” group.Click "Developer" -> Click "Visual Basic"
  3. In “Microsoft Visual Basic for Applications” editor, click “Normal” on the left side first.
  4. Then click “Modules”.
  5. Next double click “NewMacros” and you will be able to view the code on the right side area.Click "Normal" -> Click "Modules" -> Double Click "NewMacros"

In this case, the codes are as below:

 

Sub copytext()

‘ copytext Macro

    Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= _

        wdInLine, DisplayAsIcon:=False

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = “Arrow”

        .Replacement.Text = “”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = “^t”

        .Replacement.Text = “”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = ”  “

        .Replacement.Text = “”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = ” ^p”

        .Replacement.Text = “^p”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = “^p “

        .Replacement.Text = “^p”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = “^p^p”

        .Replacement.Text = “^p”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = “^p^p”

        .Replacement.Text = “^p”

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

 

Speaking of experts and advanced users who are familiar with VBA, you can totally adjust the codes to satisfy your requirements, which will be more convenient and precise.

Secure Your Files

Users of Word worldwide tend to make the best use of it. It’s a great and also a dangerous idea, since it increases the risk of collapsing Word with wrong operations. Therefore, you should always find ways to secure your files. Among the options available, getting a doc repair tool beforehand sounds not bad.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including excel recovery and pdf repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *