OpenSharedItem を使用して保存済みアイテムをインポートする

Microsoft Outlook には、iCalendar 予定 (.ics) ファイル、vCard (.vcf) ファイル、および Outlook メッセージ (.msg) ファイルを開き、そのファイルに対応する Outlook アイテムを取得する、 NameSpace オブジェクトの OpenSharedItem メソッドが用意されています。 次の表に示すように、このメソッドで取得されるオブジェクトの種類は、開く共有アイテムの種類によって異なります。

共有アイテムの種類 Outlook アイテム
iCalendar 予定 (.ics) ファイル AppointmentItem
vCard (.vcf) ファイル ContactItem
Outlook メッセージ (.msg) ファイル Type は、.msg ファイルとして保存されたアイテムの種類に対応します。

共有アイテムを開いたら、取得したオブジェクトの Save メソッドを使って、その Outlook アイテムに対応する既定のフォルダーに保存することにより、アイテムをインポートすることができます。

この例では、vCard ファイルを開き、現在のユーザーの既定の連絡先フォルダーにインポートします。

  1. NameSpace オブジェクトへの参照を取得した後、NameSpace オブジェクトの GetSharedItem メソッドを呼び出して vCard ファイルを読み込み、ContactItem の参照を取得します。

  2. 次に ContactItemSave メソッドを呼び出して、アイテムを既定の 連絡先フォルダーに保存します。

  3. 最後に、NameSpace オブジェクトの GetDefaultFolder メソッドを使用して、現在のユーザーの連絡先の既定のフォルダーへの Folder オブジェクト参照を取得し、フォルダーを表示します。

Public Sub OpenSharedContact() 
 
 Dim oNamespace As NameSpace 
 Dim oSharedItem As ContactItem 
 Dim oFolder As Folder 
 
 On Error GoTo ErrRoutine 
 
 ' Get a reference to a NameSpace object. 
 Set oNamespace = Application.GetNamespace("MAPI") 
 
 ' Open the vCard (.vcf) file containing the shared item. 
 Set oSharedItem = oNamespace.OpenSharedItem( _ 
 "C:/SampleContact.vcf") 
 
 ' Save the item to the Contacts default folder. 
 oSharedItem.Save 
 
 ' Get a reference to and display the Contacts default folder. 
 Set oFolder = oNamespace.GetDefaultFolder( _ 
 olFolderContacts) 
 oFolder.Display 
 
EndRoutine: 
 On Error GoTo 0 
 Set oSharedItem = Nothing 
 Set oFolder = Nothing 
 Set oNamespace = Nothing 
Exit Sub 
 
ErrRoutine: 
 Select Case Err.Number 
 Case 287 ' &H0000011F 
 ' This error occurs if the code is run by an 
 ' untrusted application, and the user chose not to 
 ' allow access. 
 MsgBox "Access to Outlook was denied by the user.", _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case -2147024894 ' &H80070002 
 ' Occurs if the specified file or URL could not 
 ' be found, or the file or URL cannot be 
 ' processed by the OpenSharedItem method. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case -2147352567 ' &H80020009 
 ' Occurs if the specified file or URL is not valid, 
 ' or you attempt to use the Move method on 
 ' an Outlook item that represents a shared item. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case Else 
 ' Any other error that may occur. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 End Select 
 
 GoTo EndRoutine 
End Sub

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。