Tải bản đầy đủ (.pdf) (18 trang)

Using the Visual Basic Editor

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (42.58 KB, 18 trang )

[ Team LiB ]


Using the Visual Basic Editor
Before you can write VBA macros, you need an editor. Outlook, like the other Office
applications, includes the Visual Basic Editor. Open the editor interface by choosing
Tools, Macro, Visual Basic Editor or by pressing Alt+F11 on your keyboard.

The Visual Basic Editor should be installed by default with a typical
or complete installation. If it's not available, you'll have to use the
Add/Remove Programs applet in Windows Control Panel and
change your installed features.

The Visual Basic Editor has all the tools you'll need. Use the Project Explorer to see all
the modules you've associated with Outlook and the Properties window to add or change
properties, including the project name and other properties.
Type your code in the large window. Selecting Application in the Object drop-down list,
shown as (General) in Figure 21.1
, lists the available procedures in the right field and
automatically enters the selection in the code window.
Figure 21.1. Use the Visual Basic Editor to develop your macros.

The Visual Basic Editor interface includes the Project and Properties browsers, the main
code window, and the Standard toolbar. Right-click on the toolbars and show the Edit an
d
Debug toolbars. Figures 21.2
through 21.4 detail the buttons found on these toolbars.
Figure 21.2. The Standard toolbar has the standard Windows commands along with
commands you can use to switch back to Outlook, select a module, form, or class,
run your code, and show the object browser and control toolbox.


Figure 21.4. The Debug toolbar is used for your debugging code. Use Run, Break,
and Reset to start and stop the macro, and use Step In, Out, and Over to run the
code line by line or skip lines. Use the windows and watches and see the results your
code returns.

Figure 21.3. Use the Edit toolbar to provide information about properties and
methods in your code, toggle breakpoints, change indentation, comment code
blocks, and use bookmarks while writing and debugging your code.

One of the most important features of the VB Editor is the Object Browser. Using the
Object Browser, you can see all the properties, methods, and objects referenced in your
project and available for you to use (see Figure 21.5
).
Figure 21.5. Use the Object Browser to view the object model and libraries
referenced in your project. Select the library from the Project/Library list or enter
search words in the Search field.

Add or remove libraries from your project using Tools, References.
Only references that have checkmarks are available for use in your
project.

Now that you know where to find the main parts of the editor, it's time to write your
code. The general steps are
1. Make a copy of or back up your mailbox before testing your code. Although it's
safer to test against a test account, the results might be different when you test
against real data and it's very risky if your code doesn't work correctly. Backups
are especially important when you're writing code that changes items.
2. Name the Sub procedure. This is the project name you'll look for when you run the
macro from Tools, Macros, Macros. Don't use spaces and keep the name short.
3. Declare your objects, strings, and so on. You need to define the name and data

type of a variable used in your code.
4. Type your code in the module.
5. Test your code to see if it works without error.
Use the Save button often and either exit Outlook and confirm the
prompt to save the VBAProject.OTM, or copy the code to Notepad or
an Outlook post form and then save. If Outlook hangs on your code,
you could lose your work.

Task: Create Your First Macro
When there is a feature Microsoft forgot that you'd like to have in Outlook, many times
you can create a macro yourself. Some macros are complex, but others just look complex.
The macro we're going to create falls into the second group; it looks complex, but it's
really simple.
This macro provides a time-saving feature that Outlook should have, but doesn't: the
ability to easily save attachments to a specific folder on your hard drive and then delete
the attachment from the message. In addition, we add a clickable link pointing to the
attachment on the bottom of the message.
Attachments that are blocked by Outlook's Attachment Security
features will not be saved or removed from the message.

Before you can enter VBA code, you have to open the VB Editor, using the Tools,
Macros, Visual Basic Editor menu selection. If you prefer keyboard shortcuts, use
Alt+F11.
1. Click on ThisOutlookSession to open it in the code window, if it's not already
open.
2. Choose Insert, Procedure to open the Add Procedure dialog (see Figure 21.6
).
Enter a name for the procedure. I'm calling mine SaveAttachments. Leave the
options on their defaults and choose OK. Use an underscore to replace spaces in
names.

Figure 21.6. Use the Add, Procedure dialog to enter the name of the
procedure automatically or type it in the code window yourself.

You can download a text file containing the code sample from
/>. Copy and paste the text into
ThisOutlookSession, and then follow along with the text.


3. Declare your variables. This macro removes attachments from selected messages,
so we know we'll need to define what Application, MailItem, Attachments, and
Selection are. Any variables we discover we need later will be added here.
Type the following code into your subprocedure in ThisOutlookSession, as shown
in Figure 21.7
.
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Figure 21.7. Define the objects you'll use in your macro.

4. We need a location to save the attachments. In my case, I wanted them saved to a
subfolder in My Documents. I could easily hardcode the path, but if I give it to
someone else, her path would be different. Add a line for error handling and
comment out or remove the MsgBox line when you are satisfied that it's getting

the correct path.
A hardcoded path would look something like this, where My Outlook Attachments
is the full path to the folder location:
strFolderpath = "D:\My Data\My Outlook Attachments\"
Because I want to share this code with others, I'm using the user's My Documents
folder, which is a special Windows folder whose full path is stored in the registry.
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
MsgBox strFolderpath
On Error Resume Next
Add an apostrophe to the front of a line you want to comment out. VB
ignores all comment lines, which are used as notes to the person
reading the program code. You can also select a block of code and use
the Edit toolbar's Comment Block and Uncomment Block buttons to
quickly add and remove comments from your code.
Use comments liberally throughout the code so that you can remember
why you did it like that months later.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×