>

VBA를 사용하여지정된 디렉토리에서 .msg 파일을 열려고하려고하지만 런타임 오류가 계속 발생합니다.

내가 가진 코드 :

Sub bla()
    Dim objOL As Object
    Dim Msg As Object
    Set objOL = CreateObject("Outlook.Application")
    inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+"
    thisFile = Dir(inPath & "\*.msg")
    Set Msg = objOL.CreateItemFromTemplate(thisFile)
    ' now use msg to get at the email parts
    MsgBox Msg.Subject
    Set objOL = Nothing
    Set Msg = Nothing
End Sub

런타임 오류는 다음과 같습니다 :

Run-time error '-2147287038 (80030002)':

Cannot open file: AUTO Andy Low Yong Cheng is out of the office (returning 22 09 2014).msg.

The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click properties to check your permissions for the folder.


  • 답변 # 1

    케네스 리 파일을 열 때 전체 경로가 없었습니다. 이것을 시도하십시오 :

    Sub bla_OK()
    Dim objOL As Object
    Dim Msg As Object
    Set objOL = CreateObject("Outlook.Application")
    inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+"
    thisFile = Dir(inPath & "\*.msg")
    'Set Msg = objOL.CreateItemFromTemplate(thisFile)
    Set Msg = objOL.Session.OpenSharedItem(inPath & "\" & thisFile)
    ' now use msg to get at the email parts
    MsgBox Msg.Subject
    Set objOL = Nothing
    Set Msg = Nothing
    End Sub
    
    

  • 답변 # 2

    오류가 발생하면 늦은 비딩을 시도( Dim Msg As Object ) MsgBox 바로 아래  (주석을 제거해야 함) :

    Sub Kenneth_Li()
        Dim objOL As Outlook.Application
        Dim Msg As Outlook.MailItem
        Msgbox "If you get an error, try the Late Biding right under this (need to be uncommented)"
        'Dim objOL As Object
        'Dim Msg As Object
        Set objOL = CreateObject("Outlook.Application")
        inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+"
        thisFile = LCase(Dir(inPath & "\*.msg"))
        Do While thisFile <> ""
            'Set Msg = objOL.CreateItemFromTemplate(thisFile)
            'Or
            'Set Msg = objOL.OpenSharedItem(thisFile)
            'Set Msg = GetNameSpace("MAPI").OpenSharedItem(thisFile)
            'Eventually with Shell command (here for notepad)
            'Shell "notepad " & thisFile
            Set Msg = objOL.Session.OpenSharedItem(thisFile)
    
            Msg.display
            MsgBox Msg.Subject
            thisFile = Dir
        Loop
    
        Set objOL = Nothing
        Set Msg = Nothing
    End Sub
    
    

    또는 멋진 VB 솔루션을 찾을 수 있습니다 : http://www.mrexcel.com/forum/excel-questions/551148-open-msg-file-using-visual-basic-applications.html#post2721847

    Shell 에 대한 자세한 내용은 여기를 참조하십시오.  방법 : http://p2p.wrox.com/access-vba/27776-how-open-msg-file-vbulletin.html#post138411

  • 답변 # 3

    다른 방법은 파일을 프로그래밍 방식으로 (VBA에서) 실행하는 것입니다 Shell 를 사용하십시오  명령). 항목이 열린 상태에서 활성 검사기 창을 얻을 수있는 Outlook에서 열립니다.

  • 답변 # 4

    다음 코드를 확인하고 코드를 수정할 수 있습니다

    Sub CreateFromTemplate() 
    Dim MyItem As Outlook.MailItem 
    Set MyItem = Application.CreateItemFromTemplate("C:\temp\*.msg") 
    MyItem.Display 
    End Sub
    
    

  • 답변 # 5

    이것을 시도하십시오

    Sub GetMSG()
    ' True includes subfolders
    ' False to check only listed folder
       ListFilesInFolder "C:\Users\lengkgan\Desktop\Testing", True
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
        Dim FSO As Scripting.FileSystemObject
        Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
        Dim FileItem As Scripting.File
        Dim strFile, strFileType, strAttach As String
        Dim openMsg As MailItem
    Dim objAttachments As Outlook.Attachments
    Dim i As Long
    Dim lngCount As Long
    Dim strFolderpath As String
    'where to save attachments
    strFolderpath = "C:\Users\lengkgan\Desktop\Testing"
        Set FSO = New Scripting.FileSystemObject
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        For Each FileItem In SourceFolder.Files
        strFile = FileItem.Name
    ' This code looks at the last 4 characters in a filename
    ' If we wanted more than .msg, we'd use Case Select statement
    strFileType = LCase$(Right$(strFile, 4))
      If strFileType = ".msg" Then
        Debug.Print FileItem.Path
    Set openMsg = Outlook.Application.CreateItemFromTemplate(FileItem.Path)
    openMsg.Display
        'do whatever
    Set objAttachments = openMsg.Attachments
        lngCount = objAttachments.Count
        If lngCount > 0 Then
        For i = lngCount To 1 Step -1
        ' Get the file name.
        strAttach = objAttachments.Item(i).Filename
        ' Combine with the path to the Temp folder.
        strAttach = strFolderpath & strAttach
        ' Save the attachment as a file.
        objAttachments.Item(i).SaveAsFile strAttach
        Next i
        End If
      openMsg.Close olDiscard
    Set objAttachments = Nothing
    Set openMsg = Nothing
    ' end do whatever
          End If
        Next FileItem
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                ListFilesInFolder SubFolder.Path, True
          Next SubFolder
        End If
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
    End Sub
    
    

    수정 : 참조를 추가하는 방법
    도구>참조를 클릭하십시오. 필요한 참조를 확인하십시오

  • 이전 Java에서 상수 객체를 어떻게 만듭니 까?
  • 다음 python - snakemake에서 plink를 사용하여 두 가지 규칙을 정의하는 방법