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
- Firstly, open your Word.
- 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
- Once “Developer” tab is available, click it.
- Then click “Record Macro” in “Code” group.
- Now a “Record Macro” dialog box will pop up. First, type the macro name, such as “copytext” here.
- Then in “Assign macro to” part, choose either “Button” or “Keyboard”. Here we will go “Button”.
- Next, click “OK”.
- And now you will see the “Word Options” window. Click “Customize Ribbon”.
- 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.
- Noe continue to choose “Macros” from the drop-down list of “Choose commands from”.
- Next click the macro you just create.
- Then click “Add” in the middle of the window.
- Now the macro will be placed in the new group. Just right click the macro.
- And choose “Rename” option.
- In “Rename” dialog box, choose a symbol for your macro.
- Then type the display name.
- Click “OK”.
- Now go back to “Word Options” window, and click “OK” there. The macro starts recording from now.
Paste Text as Unformatted Text
- Firstly, copy a part of a long text.
- Then click “Home” tab.
- Click “Paste”.
- Next, choose “Paste Special”.
- And choose “Unformatted Text” in “Paste Special” dialog box.
- Then click “OK”. You may get a text with formatting such as below:
Remove Repeating Words
- Next start clearing the formatting left. Press “Ctrl+ H” to open the “Find and Replace” dialog box.
- And type “Arrow” in “Find what” text box.
- Then click “Replace All”.
Remove Tab Characters
- Firstly, press “Ctrl+ H” to open the “Find and Replace” dialog box.
- Then type “^t” in “Find what” text box.
- Next click “Replace All” to remove all tab characters.
Remove Extra Blanks
- Still press “Ctrl+ H” to open the “Find and Replace” dialog box.
- In “Find what” text box, press the blank key twice.
- Then click “Replace All” button.
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.
- Similarly, press “Ctrl+ H” to open the “Find and Replace” dialog box.
- Next in “Find what” text box, press a blank key first.
- Then type “^p”.
- In “Replace with” text box, type “^p”.
- Click “Replace All”.
- Again open the “Find and Replace” dialog box.
- And in “Find what” text box, first type “^p”.
- Then press blank key once.
- Then type “^p” in “Replace with” text box.
- Finally click “Replace All” button.
Remove the Left Blank Lines If Necessary
Still, if there are blank lines left, try following steps:
- Press “Ctrl+ H” to open the “Find and Replace” dialog box.
- next type “^p^p” in “Find what” text box.
- And type “^p” in “Replace with” text box.
- Finally, click “Replace All” button.
By now, you may get result as below:
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
- Once you finish removing the text formatting, click “Developer” tab.
- Then click “Stop Recording”. Now the macro will be saved.
How to Run a Macro
- Copy the text with formatting online, say.
- Open Word next.
- Then click “Developer” tab.
- Click the macro icon and you will see the text on the document without additional 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.
- Firstly click “Developer” tab.
- Then click “Visual Basic’ in “Code” group.
- In “Microsoft Visual Basic for Applications” editor, click “Normal” on the left side first.
- Then click “Modules”.
- Next double click “NewMacros” and you will be able to view the code on the right side area.
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