본문 바로가기
프로그래밍/VBA, VBS

[VBA] eml파일 내 정보 읽어오기

by 왕초보 개발자 2021. 3. 25.
728x90

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

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Sub test()

    strMyFile = "C:\Users\user\Documentss\test.eml" ' eml 파일 경로

    If Dir(strMyFile) = "" Then
        ' eml 파일 존재하지 않을 경우에 대한 예외 처리
        MsgBox "File " & strMyFile & " does not exist"
    Else
        ShellExecute 0, "Open", strMyFile, "", strMyFile, SW_SHOWMAXIMIZED
    End If

    Set OL = CreateObject("Outlook.Application")
    Application.Wait (Now + TimeValue("0:00:03")) ' 안정적 실행을 위한 3초 대기
    Set MyInspect = OL.ActiveInspector
    Application.Wait (Now + TimeValue("0:00:03")) ' 안정적 실행을 위한 3초 대기
    Set myitem = MyInspect.CurrentItem
    Application.Wait (Now + TimeValue("0:00:03")) ' 안정적 실행을 위한 3초 대기

    MsgBox myitem.senderemailaddress ' 보낸사람
    MsgBox myitem.To  ' 받는사람
    MsgBox myitem.CC  ' 참조
    MsgBox myitem.Subject   ' 메일 제목
    MsgBox myitem.Body   ' 메일 본문 - 텍스트
    MsgBox myitem.HTMLBody  ' 메일 본문 - HTML

    ' 첨부파일 존재할 경우 지정한 폴더 안에 첨부파일 다운로드
    Set att = myitem.attachments
    If att.Count > 0 Then
        For Each i In att
            fn = "C:\Users\user\Documents\"  ' 첨부파일 저장할 폴더 경로 지정
            i.SaveAsFile fn & i.DisplayName  ' 첨부된 파일 저장
            Set i = Nothing
        Next
    End If

    myitem.Close 1
End Sub

728x90