2017-04-20 26 views
1

以下のコードは、自分の受信トレイから私のSubfolderが見つかったときにアクティブなウィンドウでメールを開きます。pdfの添付ファイルをPDFから保存する

私は"Open"に私はpdfフォームからテキストフィールドのいずれかを使用して添付ファイルを保存することができるように、この電子メールに添付pdf形をしたいと思います。

私が見つけることができる唯一のコードは、添付ファイルを一時フォルダに保存しますが、pdfフォームからコンテンツを取得しません。オフに明示的なオプションの設定

Sub OpenMailAttachment() 

    Dim ns As NameSpace 
    Dim Inbox As MAPIFolder 
    Dim openMsg As Outlook.MailItem  
    Dim mySubFolder As MAPIFolder 
    Dim myAttachment As Outlook.Attachment 
    Dim FileName As String  
    Dim myInspector As Outlook.Inspector 

    Set ns = GetNamespace("MAPI") 
    Set Inbox = ns.GetDefaultFolder(olFolderInbox) 
    Set mySubFolder = Inbox.Folders("PdfTest") 

    mySubFolder.Display 

    Set openMsg = mySubFolder.Items(1) 

    openMsg.Display 

    mySubFolder.Application.ActiveExplorer.Close 

    openMsg.Application.ActiveWindow 

    For Each myAttachment in Item.Attachment 
     FileName = "C:\temp\" & myAttachment.FileName 

     myAttachment.SaveAsFile FileName 

     myAttachment = openMsg.Attachments.Item.DisplayName 
     '(I get Compile error: *.Item* argument not optional) 

     myAttachments.Application.ActiveInspector.Display 

End Sub 
+0

https://meta.stackexchange.com/a/5235/289619 – 0m3r

答えて

1

これべきこと...

Option Explicit 
' use Declare PtrSafe Function with 64-bit Outlook 
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (_ 
    ByVal hwnd As Long, _ 
    ByVal lpOperation As String, _ 
    ByVal lpFile As String, _ 
    ByVal lpParameters As String, _ 
    ByVal lpDirectory As String, _ 
    ByVal nShowCmd As Long _ 
) As Long 

Sub OpenMailAttachment() 
    Dim ns As NameSpace 
    Dim Inbox As MAPIFolder 
    Dim openMsg As Outlook.MailItem 
    Dim mySubFolder As MAPIFolder 
    Dim Attachment As Outlook.Attachment 
    Dim myAttachments As Outlook.Attachments 
    Dim FileName As String 
    Dim myInspector As Outlook.Inspector 
    Dim Item As Object 
    Dim sFileType As String 

    Set ns = GetNamespace("MAPI") 
    Set Inbox = ns.GetDefaultFolder(olFolderInbox) 
    Set mySubFolder = Inbox.Folders("PdfTest") 

    mySubFolder.Display 

    Set openMsg = mySubFolder.Items(1) 

    openMsg.Display 
    mySubFolder.Application.ActiveExplorer.Close 
    openMsg.Application.ActiveWindow 

    Set myAttachments = openMsg.Attachments 

    If myAttachments.Count Then 
     For Each Attachment In myAttachments 
      'Last 4 Characters in a Filename 
      sFileType = LCase$(Right$(Attachment.FileName, 4)) 

      Select Case sFileType 
       ' Add additional file types below 
       Case ".pdf" ', ".doc", "docx", ".xls" 

       FileName = "C:\temp\" & Attachment.FileName 
       Attachment.SaveAsFile FileName 
       ShellExecute 0, "open", FileName, vbNullString, vbNullString, 0 
      End Select 
     Next 
    End If 


End Sub 

Option Explicit Statement (Visual Basic)

一般グーではありませんdの練習。 1つまたは複数の場所で変数名を間違えると、プログラムの実行時に予期しない結果が発生する可能性があります。

+0

ザ・は、PDFを開いていますが、私の見通しをシャットダウンし続けます。エラーメッセージ:MS Outlookが動作を停止しました。問題により、プログラムが正常に動作しなくなりました。 – CPmngr

+0

@CPmngrどのOutlookバージョンを実行していますか? – 0m3r

関連する問題