LoginSignup
0
0

More than 3 years have passed since last update.

How to use Automation to send a Microsoft Outlook message using Access 2000

Posted at

How to use Automation to send a Microsoft Outlook message using Access 2000
https://support.microsoft.com/en-us/help/209948/how-to-use-automation-to-send-a-microsoft-outlook-message-using-access
For a Microsoft Access 97 version of this article, see
161088 .
Advanced: Requires expert coding, interoperability, and multiuser skills.
とても便利:コードは無駄に長いよ、でも誰でも使えるとっても便利な技だよ!
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
accdbでも使えるよ

確かにクロスプラットフォームですが、VBSから動かしているのとたいして変わりません。むしろ定数を定義しないだけ便利で短いです。
あとmdbというわけにはいかない。自分からするとWindows APIよりははるかに簡単です。

IN THIS TASK

この記事の項目
SUMMARY
Sending a Microsoft Outlook Mail Message Programmatically
REFERENCES

Summary

This article shows you how to use Automation to create and send a Microsoft Outlook message in Microsoft Access 2000.

ぶっちゃけこのコード、Accessとは全く関係ありませんし、Accessでやる意味がないです

原文ではAccess2000で紹介しているのですが、全くそんなことはなくて、ExcelでもWordでもPowerPointでもPublisherでも同じです。
というかVBSは参照設定ができないので、CreateObjectのレイトバインディングになりますが、それと変わりません。このコードにはAccess独自の機能とか命令とかオブジェクトなど全く使っていないからです。

この時代はOutlook本体のVBAは付け足しだったことはすでにOutlook VBA Objectまとめで紹介していますが、97くらいから送信する機能はあったようです。もっともそのころから今日に至るまでOUTLOOKは重いし落ちるしいいことなんて一つもないので、大体だれも使っていないわけです。つまりOutlook以外から操作するためにVBAがあるということだったようです。しかしながら、もう一つの障害はE-mailを送信しようとするとブロックする可能性があることを示唆しています。されたことはないのですが。

マイクロソフトは、明示または黙示の保証なしに、例示のみを目的としてプログラミング例を提供しています。 これには、商品性または特定の目的への適合性の暗黙の保証が含まれますが、これらに限定されません。 この記事は、デモされているプログラミング言語と、プロシージャの作成とデバッグに使用されるツールに精通していることを前提としています。 マイクロソフトのサポートエンジニアは、特定の手順の機能を説明できますが、これらの例を変更して追加機能を提供したり、特定の要件を満たす手順を構築したりすることはありません。
注:Outlook電子メールセキュリティアップデートをインストールした場合、次のコードは正常に動作しない可能性があります。 この更新プログラムの詳細については、マイクロソフトサポート技術情報の次の記事のいずれかを参照してください。
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
NOTE: The following code may not work properly if you have installed the Outlook E-mail Security Update. For additional information about this update, please see one of the following articles in the Microsoft Knowledge Base, depending on which version of Outlook you have:
262631 OL2000: Information About the Outlook E-mail Security Update
https://support.microsoft.com/EN-US/help/262631
262617 OL98: Information About the Outlook E-mail Security Update
https://support.microsoft.com/EN-US/help/262617

すでにデッドリンクですが、番号に注目してください。31と17です。14しか離れていません。つまりこの2つのヘルプはほぼ同時に発行されたものだといえます。

You can use the SendObject method to send a MAPI mail message programmatically in Microsoft Access. However, the SendObject method does not give you access to complete mail functionality, such as the ability to attach an external file or set message importance. The example that follows uses Automation to create and send a mail message that you can use to take advantage of many features in Microsoft Outlook that are not available with the SendObject method.
SendObjectメソッドを使用して、Microsoft AccessでプログラムでMAPIメールメッセージを送信できます。 ただし、SendObjectメソッドでは、外部ファイルを添付したり、メッセージの重要度を設定したりするなど、完全なメール機能にアクセスできません。 次の例では、オートメーションを使用してメールメッセージを作成および送信します。このメッセージを使用して、SendObjectメソッドでは使用できないMicrosoft Outlookの多くの機能を利用できます。

SendObjectは

There are six main steps to sending a Microsoft Outlook mail message by using Automation, as follows:

  1. Initialize the Outlook session.
  2. Create a new message.
  3. Add the recipients (To, CC, and BCC) and resolve their names.
  4. Set valid properties, such as the Subject, Body, and Importance.
  5. Add attachments (if any).
  6. Display/Send the message.

Sending a Microsoft Outlook Mail Message Programmatically

Create a sample text file named Customers.txt in the C:\My Documents folder %USERPROFILE%\Documents.
Start Microsoft Access, and open the sample database Northwind.mdb.
Create a module and type the following line in the Declarations section if it is not already there:

Option Explicit

On the Tools menu, click References.
In the References box, click to select the Microsoft Outlook 9.0 Object Library, and then click OK.

NOTE: If the Microsoft Outlook 9.0 Object Library does not appear in the Available References box, browse your hard disk for the file, Msoutl9.olb. If you cannot locate this file, you must run the Microsoft Outlook Setup program to install it before you proceed with this example.
Type the following procedure in the new module:

原文ではこうなっていますが、まあOutlookが出てこないことはまず考えられないわけで(ふつうはOffice365使うからね)当然出てきます。しかしOutlookは2016いこうはOutlook 16.0 Object Libraryです。機能は上昇しないくせにバージョンだけは上がる。

変なコード

面白いというかこの時代は違うのかわからないのですが、上記の文章では参照設定をしろ、もし出てこなかったらディスクからインストールしろと書いています。昔はインターネットからダウンロードせず、カセットテープからじゃなかったフロッピーディスクじゃなくてブルーレイディスクからダウンロードしていました。

フロッピーバージョンもありましたが、CD-ROMをドライブにいれてインストールしていたのです。そして昔のWindowsマシンは容量が大きくなかったのでドライバなどを選択してインストールできたのです。

2010まではありました。

image.png

しかし参照設定をしているので

Dim objOutlook As Outlook.Application

通常は参照設定をしているわけですがから、自分などは一気に

Dim objOutlook As Outlook.Application : Set objOutlook = New Outlook.Application

ともっていきます。ところがこの公式のコードはレイトバインディング(CreateObjectを使うこと)するという本来は不必要といえる動作をします。

Dim objOutlook As Outlook.Application 
Set objOutlook = CreateObject("Outlook.Application")

このあとolToなどを使っているので、たしかにこのあとは参照設定を生かしていますが、ここは生かしていません。
これは思うに、Outlookがすでに起動している状態を想定しているのでしょう。インスタンスが同時に一つしか立ち上がらないため、Newをやってエラーになるのを回避していたのかもしれません。このVBAマクロはOutlookを終了させていない点も起動している状態が前提という主張の根拠の一つです。
なおこのプログラムが走っているときはOutlookを操作してはいけません。マクロがOutlookを使用しているので、誤作動を起こすためです。


Sub SendMessage(Optional AttachmentPath)
   Dim objOutlook As Outlook.Application
   Dim objOutlookMsg As Outlook.MailItem
   Dim objOutlookRecip As Outlook.Recipient
   Dim objOutlookAttach As Outlook.Attachment
   Dim NS As Outlook.NameSpace
   ' Create the Outlook session.
   Set objOutlook = CreateObject("Outlook.Application")
   Set NS = objOutlook.GetNamespace("MAPI")
   ' Create the message.
   Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

   With objOutlookMsg
  ' Add the To recipient(s) to the message.
 Set objOutlookRecip = .Recipients.Add("Nancy Davolio") `Outlookはメールアドレスではなく名前で追加しろと言っています。同性の時にアドレスだと
  objOutlookRecip.Type = olTo 'olmailrecipienttype https://docs.microsoft.com/ja-jp/office/vba/api/outlook.olmailrecipienttype

  ' Add the CC recipient(s) to the message.
  Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
  objOutlookRecip.Type = olCC

  ' Set the Subject, Body, and Importance of the message.
  .Subject = "This is an Automation test with Microsoft Outlook"
  .Body = "Last test - I promise." & vbCrLf & vbCrLf
  .Importance = olImportanceHigh  'High importance

  ' Add attachments to the message.
  If Not IsMissing(AttachmentPath) Then
 Set objOutlookAttach = .Attachments.Add(AttachmentPath)
  End If

  ' Resolve each Recipient's name.
  For Each objOutlookRecip In .Recipients
 objOutlookRecip.Resolve
 If Not objOutlookRecip.Resolve Then
 objOutlookMsg.Display
  End If
  Next
  .Send

   End With
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
End Sub

7.To test this procedure, type the following line in the Immediate window, and then press ENTER:

SendMessage "C:\My Documents\Customers.txt"

原文はこうなっていますが、My Documentsではないですよね。ドキュメントのCustomers.txtを添付ファイルにしてメールを送るときは、イミディエイトに打ち込むのは2019年現在ではこうですよ。

SendMessage "%USERPROFILE%\Documents\Customers.txt"

To send the message without specifying an attachment, omit the argument when calling the procedure, as follows:

このSubプロシージャは添付ファイルがなしでも動きます。Optinal なのでつけてもつけなくてもいいのです。

SendMessage

References

For more information about using Automation in Microsoft Access, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "Automation" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
209963 ACC2000: How to Use Automation to Add Appointments to Microsoft Outlook
209955 ACC2000: How to Use Automation to Create a New Contact Item in Microsoft Outlook

この2つのリンクはすでにありませんが、やはり、番号が近いです。また、AccessからあくまでもOutlookを使うという態度を見るとImportExportにOutlookが絡む点とかAcccessをOutlookと合わせて使おうという意図がとてもあります。なぜかExcelを使わないわけです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0