Monday, August 2, 2010

Assigning a Macro to a Shortcut Key:-

Word allows you to assign macros or commands to specific key combinations. These key combinations are referred to as shortcut keys, and when used they result in the macro or command being executed. When you first create a macro by recording it, Word gives you the opportunity to assign the macro to a specific key combination. If you later want to change the key combination, you can follow these steps:

1. Select Customize from the Tools menu. You will see the Customize dialog box.
2. Click on the Keyboard button. Word displays the Customize Keyboard dialog box. (Click here to see a related figure.)
3. Scroll through the Categories list and select the Macros category. The list at the right side of the dialog box changes to show the currently available macros.
4. In the Macros list, select the macro you want assigned to the shortcut key.
5. With the insertion pointer in the Press New Shortcut Key box, press the shortcut key you want to use. For instance, if you want to use Ctrl+Alt+J, press that.
6. Just below the Press New Shortcut Key box you can see whether the shortcut key is already assigned to a different function.
7. Click on Assign.
8. Repeat steps 4 through 7 for each change you want to make.
9. Click on Close.

Aligning a Paragraph in a Word Macro:-

Word allows a rich set of formatting attributes for text in a document. You can control the alignment of a paragraph by using the following VBA statement:

Selection.Paragraphs.Alignment = position

where position is one of the constants shown in the following table:-

Constant Result
wdAlignParagraphLeft Formats the current paragraph as left justified
wdAlignParagraphCenter Formats the current paragraph as centered
wdAlignParagraphRight Formats the current paragraph as right justified
wdAlignParagraphJustify Formats the current paragraph so it expands to the left and right margins

Adding a Macro to a Toolbar:-

For instance, you can create a macro, and then add it to a toolbar. How you do this depends on which version of Word you are using.

If you are using a version of Word prior to Word 2007, then follow these steps:

1. Choose Customize from the Tools menu. Word displays the Customize dialog box.
2. Make sure the Toolbars tab is selected. (Click here to see a related figure.)
3. In the list of toolbars, make sure there is a check mark beside the toolbar to which you want your macro added. The check mark ensures that the toolbar is displayed on the screen.
4. Click on the Commands tab.
5. In the list of Categories, choose the Macros entry. Your macros should then appear in the Commands list.
6. In the Commands list, select the macro you want assigned to a toolbar.
7. Using the mouse, drag the macro from the Commands list to the location on the toolbar where you want it to appear.
8. When you drop the macro, it appears on the toolbar.
9. To add more macros, repeat steps 6 through 8.
10. Click on Close.

Word 2007 is different; it doesn't technically have toolbars like older versions of Word had. You can't make additions or changes to the ribbon, but you can add macros to the Quick Access toolbar. Follow these steps:

1. Click the Office button and then click Word Options. Word displays the Word Options dialog box.
2. At the left side of the dialog box, click the Customize option.
3. Using the Choose Commands From drop-down list, choose Macros.
4. In the list of available macros, select the one you want assigned to the Quick Access toolbar.
5. Click the Add button. The command now appears at the right side of the dialog box.
6. Click the OK button. The command now appears on the Quick Access toolbar.

Accessing Objects in a Word Project:-

When you create a new application-level project for Word by using Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System (VSTO 2005 SE), Visual Studio automatically creates a ThisAddIn.vb or ThisAddIn.cs code file. You can access the Application object by using Me.Application or this.Application.

When you create a new document-level project for Word by using VSTO 2005, you have the option of creating a new Word Application or Word Template project. VSTO 2005 automatically creates a ThisDocument.vb or ThisDocument.cs code file in your new Word project for both Document and Template projects. You can access the Application and Document objects by using the Me or this object reference.

At first glance, there appears to be a lot of overlap in the Word object model. For example, the Document and Selection objects are both members of the Application object, but the Document object is also a member of the Selection object. Both the Document and Selection objects contain Bookmark and Range objects. The overlap exists because there are multiple ways you can access the same type of object. For example, you apply formatting to a Range object; but you may want to access the range of the current selection, a particular paragraph, section or the entire document.

The Application object contains the Document, Selection, Bookmark, and Range objects.

Word provides hundreds of objects with which you can interact. The following sections briefly describe the top-level objects and how they interact with each other. These include:

1. Application object
2. Document object
3. Selection object
4. Range object
5. Bookmark object

Object model:-

In computing, object model has two related but distinct meanings:

The properties of objects in general, in a specific computer programming language, technology, notation or methodology that uses them. For example, the Java object model, the COM object model, or the object model of OMT. Such object models are usually defined using concepts such as class, message, inheritance, polymorphism, and encapsulation. There is an extensive literature on formalized object models as a subset of the formal semantics of programming languages.
A collection of objects or classes through which a program can examine and manipulate some specific parts of its world. In other words, the object-oriented interface to some service or system. Such an interface is said to be the object model of the represented service or system. For example, the Document Object Model (DOM) [1] is a collection of objects that represent a page in a web browser, used by script programs to examine and dynamically change the page. There is a Microsoft Excel object model [2] for controlling Microsoft Excel from another program, and the ASCOM Telescope Driver [3] is an object model for controlling an astronomical telescope.

Wednesday, July 7, 2010

Variables and Constants

Variables and Constants:-
During the execution of some VBA macros there will be times when information that is either gathered from the user, returned by a function, or defined by the programmer that must be stored temporarily for use later on in the macro. Sometimes this information will change during the execution of the code and sometimes it will be static. Variables and constants are used to store this type of information. In the following macro CoName is a variable:

Sub Add_Footer()
CoName = InputBox("Name your Company?", "Add Company Name to Footer")
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = CoName
.CenterFooter = ""
.RightFooter = "&N"
End With
End Sub

Like a variable, a constant is a temporary holding place for some information that is used in a procedure. However, as the name implies a constant never changes. Constants must be declared. A declaration statement in a VBA macro is used to define the value of a constant. In the following macro the Company Name is declared in the first statement of the VBA code:

Sub Add_Footer()
Const CoName = “Accounting Software Advisor”
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = CoName
.CenterFooter = ""
.RightFooter = "&N"
End With
End Sub

The company name is enclosed in quotation marks. All literal text (this is what strings are referred to in VBA programming), which is placed in a procedure, must be surrounded by quotation marks.

Function & Procedure

Functions:-
A VBA function is a lot like a workbook function in an Excel spreadsheet. It performs a calculation and then returns the appropriate result. Functions provide information that is useful in building VBA procedures. In the previous example, the Msgbox function was used to display the path information on the screen.

VBA Functions should not be confused with Function Procedures. A Function Procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but a function can also return a value. A Function procedure can take arguments, such as constants, variables, or expressions that are passed to it by a calling procedure. If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. A function returns a value by assigning a value to its name in one or more statements of the procedure.

Procedures:-
When you create VBA code inside an Excel workbook it will be stored as a “Module” within the workbook. Unlike, Lotus 1-2-3 macros, these modules are not entered in cells but are still a part of the workbook files. They can only be viewed by using the VBA Editor.


Code within a module is organized into procedures. A procedure tells the application how to perform a specific task. Use procedures to divide complex code tasks into more manageable units. There are three types of VBA procedures: Sub, Function, and Property.

The most common type of procedure is the Sub. A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements that performs actions but doesn't return a value. A Sub procedure can take arguments, such as constants, variables, or expressions that are passed by a calling procedure. If a Sub procedure has no arguments, the Sub statement must include an empty set of parentheses. The following is an example of a simple Sub:

Sub Center_Across_Selection()

' Center_Across_Selection Macro
' Macro recorded 4/19/2003 by Carlton Collins
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Selection.Style = "Comma"
Selection.Font.Underline = xlUnderlineStyleSingleAccounting
End Sub