Xem mẫu

  1. [ Team LiB ] Understanding Outlook's VBA Programming and VBA scares a lot of people off. It's really not hard and many code samples are available on the Internet for you to use, so you don't even have to know how to program. But if you're unsure of whether you're ready for this, put it down, get yourself an introductory Visual Basic programming book, and come back to this hour when you feel you're ready. Typing code samples from books isn't a favorite hobby of mine, even for short code snippets. It's too easy to make a typing error and hard to notice the mistake when the code errors, resulting in frustration and a feeling that you can't do it. For this reason, I have a text file with the code used in this book available at www.poremsky.com/samples/ for you to download. Copy and paste the code into the VB Editor as you read this hour and you'll discover you can do it. You might even discover you like doing it. There are many differences between how you create macros in Outlook compared to Word and Excel. First, you have to write the code yourself; you can't record it using a Macro recorder. Secondly, Word and Excel enable you to create a lot of modules that you can easily distribute to others. Outlook uses one module, C:\Documents and Settings\username\Application Data\Microsoft\Outlook\VBAProject.OTM, which contains all the macros the user created. All forms and modules are stored within this one file. If you give this file to someone and he uses it to replace his copy, he loses all previous macros he had. Projects and Modules All OutlookVBA code is contained in a single project, coincidently named VBAProject.OTM. This project is associated with a Windows user account, so all users of a computer have their own VBAProject. You can have just one VBAProject and all code used with Outlook is contained in this file. A project contains code modules and UserForm modules. Don't confuse Outlook forms with UserForms. UserForms are used to create the dialog boxes you respond to, not only in Outlook, but in all Windows programs. Code modules contain your VBA code, which is also referred to as a routine,
  2. procedure, macro, function, or subroutine. To the untrained eye, these all mean the same thing—a bunch of code "that's all Geek to me." Seasoned developers use the terms to describe different types of code. In Outlook, the main code module is called ThisOutlookSession. For a list of Outlook programming resources and code samples, see http://www.slipstick.com/dev/index.htm. There's so much to learn about writing your own Outlook code that it's better suited for a book specifically about Outlook programming. For that reason, I won't go over the object model or programming in depth; rather, I'll begin with entering your first macro. If you want to learn more, there are many resources and code samples available on the Internet. Because Outlook can do so much with code, Microsoft added security features to the object model to block unauthorized access to a number of Outlook features by other programs and VBA. Outlook 2003 eliminates the security prompts in properly constructed Outlook COM add-ins and published Outlook forms. Code in Outlook VBA also does not trigger security prompts because it's implemented as a COM add-in. However, using the code to access other COM add-ins or libraries will trigger the security warning. [ Team LiB ]