مانند اکثر برنامه های آفیس ، مایکروسافت چشم انداز 2000 و بعد از آن نسخه های نرم افزاری شامل ویژوال بیسیک ، جایی که شما نمی توانید کدهای خود را به طور خودکار به چشم انداز بنویسید. And, like most of the Office programs, there is no macro recorder. و مثل اکثر از برنامه های آفیس ، هیچ ضبط ماکرو وجود دارد. Fortunately, there are plenty of VBA samples here and elsewhere. خوشبختانه ، مقدار زیادی از نمونه VBA وجود دارد در اینجا و جاهای دیگر.
Getting Started شروع

Here are the absolute basics for getting started with Outlook VBA: در اینجا اصول مطلق برای کار با چشم انداز VBA شده :

  1. In Outlook 2000 to 2003, choose Tools | Macro | Security and set security to Medium . در چشم انداز 2000-2003 ، انتخاب ابزار | کلان | امنیت و امنیت را به مجموعه ای متوسط. In Outlook 2007, the macro security settings are in the Tools | Trust Center dialog. در چشم انداز 2007 ، تنظیمات امنیتی ماکرو گفت و گو در ابزار | مرکز تراست. Set macro security to Warn on all macros . امنیتی ماکرو را به در اخطار در تمام میدهند.
  2. Restart Outlook. چشم انداز راه اندازی مجدد.
  3. Press Alt + F11 to bring up the VBA environment, مطبوعات دگرساز + F11 را تا محیط VBA ،
  4. Expand the Project Explorer at upper left. گسترش پروژه اکسپلورر در سمت چپ بالا.
  5. Double-click the built-in ThisOutlookSession module to open it. دوبار کلیک کنید ساخته شده در ThisOutlookSession ماژول به آن باز شود. Accept the prompt to enable macros. پذیرش سریع برای فعال میدهند.
  6. Type or paste your code into the ThisOutlookSession module. تایپ یا پیست کنید کد خود را در ماژول ThisOutlookSession.

Here is a simple sample you can use to see if it's all working. در اینجا نمونه ساده شما می توانید استفاده کنید تا ببینید که آیا آن کار تمام است. It creates a new mail message with the subject "Hello World" and displays it: آن را ایجاد پیام جدید با ایمیل با موضوع "سلام جهان" و نمایش آن :

 Sub HelloWorldMessage() Dim msg As Outlook.MailItem Set msg = Application.CreateItem(olMailItem) msg.Subject = "Hello World!" msg.Display Set msg = Nothing End Sub زیر HelloWorldMessage () کم نور به عنوان Outlook.MailItem msg msg = تنظیم Application.CreateItem (olMailItem) msg.Subject = "سلام جهان!" msg.Display msg = تنظیم پایان هیچ چیز فرعی

برای اجرای ماکرو VBA VBA از محیط زیست ، F5 را فشار و یا کلیک بر روی دکمه اجرای در نوار ابزار. Or run it from the main Outlook window with Alt-F8. یا اجرای آن را از پنجره اصلی چشم انداز با دگرساز - F8 را بزنید. If you get an "enable macros" prompt, choose to enable. اگر شما "را فعال ماکرو" اعلان را انتخاب کنید برای فعال.

About the Application Object درباره برنامه شیء

Application is an intrinsic object that represents the running Outlook application. برنامه شی ذاتی است که نشان دهنده چشم انداز برنامه در حال اجرا می باشد. To avoid security prompts , you should derive all Outlook objects in your code from this intrinsic Application object, as in this statement that creates a new mail message: برای جلوگیری از امنیتی دهید ، شما باید شی مشتق چشم انداز تمام اشیاء خود را در کد برنامه ذاتی از این ، همانطور که در این بیانیه که ایجاد یک پیام ایمیل جدید :

Set msg = Application.CreateItem(olMailItem) تنظیم msg = Application.CreateItem (olMailItem)

For an example of how to get a "trusted" MailItem object derived from Application when writing a procedure for use with an Outlook "run a script" rule action, see: برای مثال از چگونگی دریافت "اعتماد" MailItem شیء بدست آمده از برنامه هنگام نوشتن روش برای استفاده با چشم انداز "اجرای اسکریپت" اقدام حکومت ، نگاه کنید به :

If you want to write a macro that operates on either an open item or an item selected in a folder, you can add the GetCurrentItem() function to your VBA project. اگر می خواهید برای نوشتن یک ماکرو که عمل در هر دو آیتم باز کردن و یا آیتم انتخاب شده در پوشه ، شما می توانید اضافه کردن GetCurrentItem تابع () به VBA پروژه شما. Just remember to change this statement: فقط این بیانیه به تغییر یاد داشته باشید :

Set objApp = CreateObject("Outlook.Application") objApp تنظیم = CreateObject ("Outlook.Application")

to: به :

Set objApp = Application objApp تنظیم = کاربرد

so that it uses the intrinsic Application object. به طوری که آن را با استفاده از برنامه ذاتی شی.

To process all the items selected in Outlook's main window, first return the ActiveExplorer.Selection collection: به فرایند تمام آیتم های انتخاب شده در پنجره اصلی چشم انداز است ، بازگشت به ابتدا مجموعه ActiveExplorer.Selection :

Set colSel = Application.ActiveExplorer.Selection تنظیم colSel = Application.ActiveExplorer.Selection

To perform some action on the currently open item, first return the ActiveInspector.CurrentItem object: برای انجام برخی اقدامات در حال حاضر مورد باز ، 1 بازگشت شی ActiveInspector.CurrentItem :

Set objItem = Application.ActiveInspector.CurrentItem objItem تنظیم = Application.ActiveInspector.CurrentItem