Microsoft Word Vba Examples

PRODUCT DISCLOSURE $

Each VBA Tutorial contains macro examples to help you learn VBA code quickly. Follow these VBA tutorials to learn basic and advanced Excel macro programming.

A macro is a series of commands that is recorded so it can be played back, or executed, later. Creating and running macros isn't too difficult, and the resulting efficiency is well worth the time spent learning to use them. This tutorial covers macros in Word 2003. If you have a later version, you can record macros in Word 2007, and create macros in Word 2010 and later.

  1. Visual Basic for Applications (VBA), the programming language for Word macros, and see examples of recording and adapting macros. Participants will also receive a WWW listing of downloadable macros and a bibliography of relevant printed and on-line publications.
  2. The Office 2013 VBA Documentation download provides an offline version of the Visual Basic for Applications (VBA) developer reference for each of the Office client applications, as well as the VBA reference content shared amongst all Office client applications (Office Shared).
  3. The names you use for macros can affect what you see when you add those macros to a toolbar. This tip explains how you can change macro names on toolbars, as well as change the ToolTip that appears for a macro. Invisible Macros When configuring Word, you may want to add macros to either menus or toolbars.

Creating Macros

There are a couple of different ways to create Word macros: The first and easiest way is to use the macro recorder. The second way is to use VBA or Visual Basic for Applications. Further, Word macros can be edited by using the VBE, or Visual Basic Editor.

There are more than 950 commands in Word, most of which are on menus and toolbars and have shortcut keys assigned to them. Some of these commands, however, are not assigned to menus or toolbars by default. Before you create your own Word macro, you should check to see if it already exists and can be assigned to a toolbar. To see the commands available in Word, follow these steps:

  1. Select the Tools menu and choose Macro.

  2. Select Macros from the submenu.

    You can also use the Alt + F8 shortcut key to access the Macrosdialog box.

  3. Select Word Commands in the drop-down menu beside Store Macro in.

  4. An alphabetical list of the command names will appear. If you highlight a name, a description of the command will appear at the bottom of the box under the Description label.

If the command you wish to create already exists, you should not duplicate it with your own Word macro. If it doesn't exist, proceed with creating your Word macro.

Planning Effective Word Macros

The most important step in creating effective Word macros is careful planning. While it might seem a bit obvious, you should have a clear idea of what you want the Word macro to perform, how it will make your future work easier, and the circumstances under which you intend to use it. Otherwise, you may end up spending time creating an ineffective macro that you won’t use.

Once you have these things in mind, it is time to plan the actual steps. This is important because the recorder will remember everything you do and include it in the macro. For example, if you type something and then delete it, every time you run that macro, Word will make the same entry and then delete it, making a sloppy and inefficient macro.

When you are planning your macros, here are some things to consider:

  • Plan the commands and the order in which you want the macro to perform them.
  • Know the shortcut keys for the commands you plan to use. This is particularly important for navigation; you will not be able to use the mouse for navigation within the document area when you are running the recorder. Further, you will create a leaner macro if you use a shortcut key rather than the arrow keys.
  • Plan for messages that Word might display and that will stop the macro.
  • Use as few steps as possible to keep the macro efficient.
  • Do at least one test run before you start recording.

After you’ve planned your Word macro and done a run-through, you are ready to record it. If you’ve planned your macro carefully enough, recording it for later use will be the easiest part of the process. It is so easy, in fact, that the only difference between creating a macro and working on the document is that you have to press a few extra buttons and make a couple of selections in dialog boxes.

Setting Up Your Macro Recording

Record your macro

The mouse pointer will now have a small icon that looks like a cassette tape beside it, indicating that Word is recording your actions. You can now follow the steps you laid out in the planning stage; once you are done, press the Stop button (it is the blue square on the left). Once you press the Stop button, your Word macro is ready to use.

  1. First, select the Tools menu and choose Record New Macro.. to open the Record Macro dialog box.

    With big files, cmp will be a lot faster than COMP or FC.That is if you need it to be a command-line tool. As far as I could tell, Windows' FC and COMP both also read the entire files, even if there was a difference in the first bytes.As John T suggests, get GNU diffutils, which includes cmp.exe. Gnu diff windows. The distinction is important if the files are big.Checksumming methods will obviously read both files entirely. He only wants to know IF 2 files are different. If you can use a GUI shareware, Total Commander has a 'Compare Files' command, and a 'Synchronize dirs' command which can be set to compare files by content.

  2. Type a unique name in the box beneath Macro Name and enter a description of the actions the macro performs in the Description box.

    Names can contain up to 80 letters or numbers (no symbols or spaces) and must begin with a letter. The name you assign the macro should be unique enough that you remember what it does without having to refer to the description.

  3. Select whether you want the macro to be available in all documents or only in the current document. If you choose to limit the availability of the command, highlight the document name in the Store Macro in drop-down box.

    By default, Word makes the macro available to all your documents, and you will probably find that this makes the most sense.

  4. When you have entered the information for the macro, select OK. The Record Macro Toolbar will appear in the upper left corner of the screen.

If, for any reason, you need to pause the recording, click the Pause Recording/Resume Recorder button (it is the one on the right). To resume recording, click it again.

Microsoft Word Vba Examples

Test Your Macro

The purpose behind creating macros in Word is to speed up your work by putting repetitive tasks and complex sequences of commands at your fingertips. What could take literally hours to do manually only takes a few seconds with the click of a button.

Apr 04, 2017  Scribd adalah sebuah pembaca PDF dan ePub dengan pustaka virtual yang terintegrasi. Kamu dapat menggunakannya untuk membaca semua jenis dokumen tertulis dan untuk mengakses jutaan buku yang dapat diunduh langsung melalui aplikasi itu sendir. Join the millions of readers who turn to Scribd’s digital library to access an unlimited. number of audiobooks, books, magazine articles, sheet music, and more. Our ad-free experience puts some of the best books and audiobooks in your pocket starting with a free 30 day trial for new members. GET STARTED TODAY: 1. Download the free Scribd app. Download Scribd Documents, Issuu Magazines quickly for free. Download the Scribd app for the best mobile reading experience. Download Now. Stay Informed. The information you need from the journalists you trust. Membership Details. Access an unlimited. number of books, audiobooks, magazines, and more — all for only $8.99/month. BooksLearn, escape. /download-aplikasi-scribd.html.

  1. To run your macro, use the Alt + F8 shortcut key to bring up the Macros dialog box.

  2. Highlight your macro in the list and then select Run.

  3. If you don’t see your macro, make sure the correct location is in the Macros in box.

Creating Keyboard Shortcuts for Macros

Of course, if you’ve created a lot of macros, searching through the Macros dialog box will eat up a lot of the time you save. If you assign your macros a shortcut key, however, you can bypass the dialog box and access your macro directly from the keyboard in the same way you can use shortcut keys to access other commands in Word.

  1. From the Tools menu, select Customize.

  2. In the Customize dialog box, select Keyboard. The Customize Keyboard dialog box will open.

  3. In the Macros scroll box, find the name of the macro to which you would like to assign the shortcut key.

  4. If the macro currently has a keystroke assigned to it, the keystroke will appear in the box below the Current keys label.

  5. If no shortcut key has been assigned to the macro, or if you would like to create a second shortcut key for your macro, select the box below Press new shortcut key.

  6. Enter the keystroke you would like to use to access your macro.

    If the shortcut key is already assigned to a command, a message will appear that says Currently assigned to followed by the name of the command. You can reassign the keystroke by continuing, or you can select a new keystroke.

  7. In the Save changes in drop-down box, select Normal to apply the change to all documents created in Word.

    To use the shortcut key only in the current document, select the document name from the list.

  8. Select Close and then choose Close on the Customize dialog box.

Hi,

Borrowing heavily from that link you posted, take a look at the code below.

Sub PopulateWordDocFromExcel()
' Add a reference to the Word-library via VBE > Tools > References > Microsoft Word xx.x Object Library.
' Create a folder named C:Temp or edit the filnames in the code.
'
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim bWeStartedWord As Boolean
Dim i As Integer
On Error Resume Next
Set wrdApp = GetObject(, 'Word.Application')
On Error GoTo 0
If wrdApp Is Nothing Then
Set wrdApp = CreateObject('Word.Application')
bWeStartedWord = True
End If
wrdApp.Visible = True 'optional!
Set wrdDoc = wrdApp.Documents.Add ' create a new document
' or open an existing document:
'Set wrdDoc = wrdApp.Documents.Open('C:FoldernameFilename.docx')

' example word operations:
With wrdDoc
'copy data from A1:A10 into the word doc:
For i = 1 To 10
.Content.InsertAfter Range('A' & i).Value
.Content.InsertParagraphAfter
Next i
wrdApp.DisplayAlerts = wdAlertsNone
.SaveAs 'C:TempMyNewWordDoc.docx', FileFormat:=12 'wdFormatXMLDocument
.Close ' close the document
wrdApp.DisplayAlerts = wdAlertsAll
End With
If bWeStartedWord Then wrdApp.Quit 'close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

If there are other things you want to do in Word, just record a macro in Word and translate the code over to this macro using wrdApp instead of Application and wrdDoc instead of ActiveDocument.

Any questions?

Ms Word Vba Macro Examples

Cheers
Rich