Written by Allen Wyatt (last updated December 2, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
Charles created a macro to insert lorem ipsum text in a document. He turned on the macro recorder, typed =lorem(), and then pressed Enter. This, of course, inserted the lorem ipsum text. He placed a shortcut to the macro on the Quick Access Toolbar, but the recorded macro only partially works. It types =lorem() for him, but he still needs to manually press Enter. Charles has tried to edit the macro to have it include the Enter command so that when he clicks on the macro shortcut the text gets entered, but to no avail.
If you want your macro to simulate pressing Enter, then you'll need to use the SendKeys command. Chances are good that when you recorded your macro, it looks very similar to this:
Sub Macro1() ' ' Macro1 Macro ' ' Selection.TypeText Text:="=lorem()" Selection.TypeParagraph End Sub
What you need to do is to replace the last line of the macro (the one just before End Sub) to either of the following:
SendKeys "~", True SendKeys "{ENTER}", False
Either of these lines (you only need one of them) will simulate pressing Enter. There is one gotcha to be aware of, however—SendKeys works in whatever window is currently active. This means you cannot run the macro from within the Visual Basic Editor and get the desired effect. The reason is because when you do that, it is the window in the Editor that is active, so SendKeys "presses Enter" within that window. You should only run the macro when your Word document window is active.
That being said, there is another way you could approach the macro, without using the SendKeys command. All you need to do is to create a new Word document that contains your lorem ipsum text, followed by whatever you want. Name this something unique, such as LoremText.docx. Then, create a macro like this one:
Sub Lorem() Selection.InsertFile FileName:="C:\Users\User\Desktop\LoremText.docx" End Sub
You just need to make sure that the path and filename shown in the macro represents the actual path and filename appropriate for your system. When you run the macro, Word goes out and grabs the LoremText.docx document and inserts it at the insertion point.
Finally, there is another way you could approach this issue without the need of resorting to macros. You could, if desired, insert the lorem ipsum text into your document, select it, and save it as a Building Block or as an AutoCorrect entry. You can then easily insert the text—modified in whatever way you desire—using these Word tools. (How to create and use both Building Blocks and AutoCorrect entries has been covered in other WordTips.)
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13484) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
Most people use Word to create regular documents that you edit, view, and print. The program also allows you to create a ...
Discover MoreSome people, while developing documents, like to use the Highlighter tool quite a bit. It can quickly get monotonous, ...
Discover MoreWhen you instruct Word to tell you how many words are in a document, it treats hyphenated words or phrases as if they are ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-12-04 09:51:12
Andrew
Here's how I do it:
Sub Lorem()
Selection.Text = Replace("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue " & _
"massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna " & _
"eros quis urna.#Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.#Pellentesque habitant morbi " & _
"tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.#" & _
"Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.#Suspendisse dui purus, scelerisque at, vulputate " & _
"vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy.#", "#", vbCr)
Selection.NoProofing = True
Selection.Collapse wdCollapseEnd
End Sub
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments