2017-05-02 2 views
1

저는 열 필요가있는 약 .msg, outlook 파일이 있으며, Excel 첨부 파일을 .csv 파일로 변환하고 저장하지 않아도됩니다. 현재 아래의 코드는 .msg outlook 파일을 열면 오류가 나타납니다. enter image description here.msg 파일을 열 수 없습니다.

.msg 파일을 열려면 어떻게합니까?

스크립트 :

Sub OpenMSGRenameDownloadAttachement() 

    Dim objOL As Outlook.Application 
    Dim Msg As Outlook.MailItem 

    Dim MsgCount As Integer 

    Set objOL = CreateObject("Outlook.Application") 

    'Change the path given month, ie. do this for Jan, Feb, April 
    inPath = "C:\January Messages" 

    thisFile = LCase(Dir(inPath & "\*.msg")) 
    Do While thisFile <> "" 

     Set Msg = objOL.Session.OpenSharedItem(thisFile) 

     Msg.Display 

     MsgBox Msg.Subject 
     thisFile = Dir 
    Loop 

    Set objOL = Nothing 
    Set Msg = Nothing 

End Sub 
+1

나는 이것이 명백한 질문이지만, 파일이 이미 열려있는 것 같아요? 예 : Outlook에서? 또는 Excel에서 이전 코드 시도가 실패한 경우? 또는 이전에 성공적으로 시도한 Excel에서? (명시 적으로'Close'를하고있는 것처럼 보이지 않으므로 여전히 열려있을 수 있습니다.) 또한이 페이지가 적용됩니까? https://support.microsoft.com/en-us/help/2633737/the -openshareditem-method-for-outlook-hold-a-file-on-signed-.msg-files – YowE3K

+0

이것은 한 번 할 일입니다. 적어도 전자 메일을 열려면이 코드를 다시 작성해야합니다. 거기에서 Excel 코드를 변환 할 수는 있습니다. – Sauron

+0

Dir을 잘못 사용하고 있습니다. 'thisFile = Dir (inPath)'와'Do While thisFile <> ' "'''If (IfFile, 3) ="msg "Then'이면 If 조건을 넣고 실제로 메시지를 열 필요는 없습니다. 내가 아는 한 첨부 파일을 얻으십시오. – Tehscript

답변

5

이 시도 :

Sub OpenMSGRenameDownloadAttachement() 
Dim Msg As Outlook.MailItem 
Dim objAtt As Outlook.Attachment 
Set objOL = CreateObject("Outlook.Application") 
Set objNs = objOL.GetNamespace("MAPI") 
'objNs.Logon 

inPath = "C:\January Messages\" 
outPath = "C:\January Messages\attachments\" 'create this folder for attachments or use your own 
thisFile = Dir(inPath & "*.msg") 

Do While Len(thisFile) > 0 
    Set Msg = objNs.OpenSharedItem(inPath & thisFile) 
    'MsgBox inPath & thisFile 
    'MsgBox Msg.Subject 
    'MsgBox Msg.SenderEmailAddress 
    'MsgBox Msg.Recipients.Item(1).Address 
    For Each objAtt In Msg.Attachments 
     If Right(objAtt, 4) = "xlsx" Or Right(objAtt, 3) = "xls" Then 
      objAtt.SaveAsFile outPath & Split(objAtt.DisplayName, ".")(0) & ".csv" 
     End If 
    Next 
    thisFile = Dir 
Loop 

Set objOL = Nothing 
Set objNs = Nothing 
End Sub 
+0

이것은 작동하지 않았고 아무 것도 실행되지 않았을 때 – Sauron

+0

'Debug.Print'를'Msgbox'로 변경합니다. – Tehscript

+0

이 또한 작동하지 않습니다. – Sauron