The double quotes tell VBA that you want to insert the string of text between them. The method’s parameter (in parentheses) is the actual text that will be inserted.InsertAfter is a method of the Selection object that inserts whatever text you specify after the selection.If nothing is selected, it represents the position of the cursor. Selection is a VBA object that represents whatever is selected at the time it is used.I think you’ll agree that it’s not too difficult. Result: The script writes the text ‘Hello World!’ into Document1.Ĭongratulations! You have just created and run your first Word VBA macro. Alternatively, from the menu, choose Run > Run Sub/User Form. Now, with the cursor inside the subroutine, click ( Run Sub/UserForm) on the toolbar. It helps you avoid spelling and syntax errors. This is a feature that Microsoft calls IntelliSense. , the editor displays a drop-down list with possible alternatives. Sub WriteText () Selection.InsertAfter ( "Hello World!" ) End Sub Typically, you choose a name that closely relates to the task you want to perform as demonstrated in the following examples. Create a subroutine by typing Sub, where can be anything other than the few reserved keywords that VBA uses. Create a Subroutine – In Word VBA, the simplest way to perform a task is to use a subroutine.Right-click Project (Document1) and choose Insert Module. This is where you add your code.To do that, you must create a placeholder into which you can put your code. Project (Document1) is the blank document that you just created. Normal is the template that most Word documents are based on by default you can ignore it in this context. Create a Placeholder (Module) for Your Code – In the VBA Editor, on the left, you see two top-level folders called Normal and Project (Document1).It may seem intimidating at first, but don’t be discouraged - it is much easier than you think! The Microsoft Visual Basic for Applications window (the VBA Editor) opens. Open the VBA Editor – Create a blank document in Word and press Alt+F11. There are three things you need to do to start using VBA:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |