Using VBA in Word to Paste and Enlarge Text from a Webpage with Small Text

In this lesson we'll be using VBA in Word to paste copied text from a webpage that has small print so we can read it more easily. If you don't have a page with small text handy, you can copy any text from this page. Although this text isn't small, the macro we'll create will work on any text we may have copied to the Windows clipboard.

Highlight a few lines of text now and either right click anywhere on the highlighted text and choose Copy from the context (popup) window, or press CTRL + c on your keyboard (technically, with this notation of pressing two keys at once, you hold down the first key - CTRL - and press the key noted after the +. Usually pressing them at the same time will work also), or go to the Edit menu and choose Copy.

Now, in a new Word document, start the macro recorder Tools>Macro>Record New Macro, remember its name, and press Enter.
[put link to instructions for button here]

1.    Paste the copied text in the document, Edit menu>Paste.

2.    Highlight all the text, Edit>Select All.

3.    Change the font size to a larger font.

4.    Stop the recorder.

5.    Press Delete to delete all the text.

6.    Paste the original copied text in the document, Edit menu>Paste.

7.    Press Enter to start a new line.

8.    Copy some different text from this page.

9.    In the document, Go to Tools>Macro>Record New Macro...

10.    Choose the macro you just recorded from the list.

11.    Press Run.

You should now see how this newly created macro can help you if you find a good webpage and the text is too small.

Open the code Editor, Tools>Macro>Visual Basic Editor and scroll down to the last Sub block (or if you changed the name of the macro, find it).

It should look like this:

Sub Macro5()

' (The lines that start with an apostrophe are comments

' and are not executed and have been removed)
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.Font.Size = 16
End Sub

Using VBA in Word - Part 2

1.    Add another row before the last row (End Sub).

2.    Type msgbox on the new line. You don’t have to align the word.

3.    Press the spacebar once and you should see the following:

Using VBA in Word

Let’s look at this a little more closely than in the previous lessons. These are arguments that VBA needs to execute this statement. They are separated by commas and the ones that are enclosed in square brackets [ ] are optional. This means that if supply only the first one, “Prompt”, we won’t get a syntax error when we run the code.

4.    Prompt, represents the text that will be displayed when this command is run and all text, as opposed to numbers, VBA keywords, etc. must be enclosed in quotation marks. Now, type “Done” (with the quotes) so when the code runs it will display the message “Done” (without the quotes) after it pastes our copied text.

5.    Because the next arguments are enclosed in square brackets, we could leave this statement as is. To show you how all this help the editor is proving eliminates a lot of memorizing all sorts of  VBA Keywords, let’s add a comma followed by a space after MsgBox "Done". This help is called Intellisense.

6.    Notice here,

Using VBA in Word

You can see that the word Prompt is no longer highlighted and the words “Buttons As …. vbOKOnly” is. Move down the list with the Down arrow key to vbCritical and press the comma key again. You should now have this:

Using VBA in Word

7.    Now the word [Title] is in bold. The title is what will be displayed at the very top of the message in the window’s border. Type something with quotes, like “My Message”. Don’t add a comma afterwards, the rest of the arguements are also optional. We'll do without them. 

Using VBA in Word - Part 3

8.    Here, we’ll take the time to learn another useful aspect of the editor. Make sure the cursor is blinking on one of the lines in this block of code, anywhere from Sub to End Sub. If it is still on the line with Msgbox, that fine. 
By the way, if you move the cursor off that line with an Up or Down arrow, or click on another line, you will see that the editor has check the line for any syntax error and formatted it’s Keyword(s). It has changed Msgbox to MsgBox.

10.    OK, with the cursor in between (inclusively) the Sub and End Sub line, press F5. This will run the code in the block. You should see the message box. Click OK to close it.

11.    You should have been brought back to the Editor after closing the message box. Press F8. This will run the code one line at a time. This is called being in debug mode and is very helpful if your trying to figure out why your code isn’t doing what you had intended. We won’t go into this in this lesson, but it’s like stopping a video to get a closer look at a frame.

End of the Using VBA in Word Tutorial

This site is powered by Site Build It!. If you enjoy it, please check out the
Site Build It homepage
 to learn more and on how to build a success-guaranteed site with no technical skills.


Go to the next tutorial: Using a VBA Input Box

Return from Using VBA in Word to Free VBA Tutorials

Return to our Homepage

Save Time Footer