2021年6月26日

[VBS] 以 VBScript 寄一封信

 

VBScript 寄一封信

https://docs.microsoft.com/en-us/office/vba/api/overview/outlook/object-model

1. Create Outlook Application

Outlook VBA 文件中所提道的 Application :

Application object (Outlook)

Represents the entire Microsoft Outlook application.

Remarks

This is the only object in the hierarchy that can be returned by using the CreateObject method or the intrinsic Visual Basic GetObject function.

The Outlook Application object has several purposes:

·                     As the root object, it allows access to other objects in the Outlook hierarchy.

·                     It allows direct access to a new item created by using CreateItem, without having to traverse the object hierarchy.

·                     It allows access to the active interface objects (the explorer and the inspector).

When you use Automation to control Outlook from another application, you use the CreateObject method to create an Outlook Application object.

Example

The following Visual Basic for Applications (VBA) example starts Outlook (if it's not already running) and opens the default Inbox folder.

VBCopy

Set myNameSpace = Application.GetNameSpace("MAPI")

Set myFolder= _

myNameSpace.GetDefaultFolder(olFolderInbox)

 

myFolder.Display

The following Visual Basic for Applications (VBA) example uses the Application object to create and open a new contact.

VBCopy

Set myItem = Application.CreateItem(olContactItem)

myItem.Display

 

 

 

要以以下形式呈現:

 

Dim oOutlook

Set oOutlook = CreateObject("Outlook.Application")

 

Dim oNameSpace

Set oNameSpace = oOutlook.GetNameSpace("MAPI")

 

 

VBScript CreateObject 開啟 Outlook.Application

 

Application 的屬性/方法 則以 oOutlook.XXXX 取得

 

Application.Quit / oOutlook.Quit 會離開 outlook 程序

2. Create Name Space

NameSapce(“MAPI”) 提供 Account 相關的一些功能。

Dim oNameSpace

Set oNameSpace = oOutlook.GetNameSpace("MAPI")

 

我們這邊需要在 Outlook 結束時寄出信件,因此以以下流程傳送與接收所有信件:

oNameSpace.SendAndReceive(FALSE) ' add this line can success the send after mail write.

 

3. Create a Mail

Dim oMail

Set oMail = oOutlook.CreateItem(0)

 

Application.CreateItem(id) 可以建立 output look 的相關項目:

expression. CreateItem( _ItemType_ )

expression A variable that represents an Application object.

 

Item Type:

OlItemType enumeration (Outlook)

Indicates the Outlook item type.

OLITEMTYPE ENUMERATION (OUTLOOK)

Name

Value

Description

olAppointmentItem

1

An AppointmentItem object.

olContactItem

2

ContactItem object.

olDistributionListItem

7

DistListItem object.

olJournalItem

4

JournalItem object.

olMailItem

0

MailItem object.

olNoteItem

5

NoteItem object.

olPostItem

6

PostItem object.

olTaskItem

3

TaskItem object.

 

Mailitem 的相關功能如下:

 

Dim oMail

Set oMail = oOutlook.CreateItem(0)

 

oMail.Subject = "Test to send a mail by outlook"

oMail.To = "jau-chih.tseng@ite.com.tw"

 

oMail.BodyFormat = 2

'Name   Value   Description

'olFormatHTML  2       HTML format

'olFormatPlain 1       Plain format

'olFormatRichText      3       Rich text format

'olFormatUnspecified   0       Unspecified format

 

oMail.HTMLBody = "<font color="&""&"red"&""&">"&_

                              "this is a test</font><br>"

 

oMail.HTMLBody = oMail.HTMLBody &_

        "<a href="&""&"\\ite00925-w7\release\hdmi1.4"&""&"> \\ite00925-w7\release\hdmi1.4 </a><BR>"

 

oMail.Send

 

沒有留言: