在目录中打开文件错误“找不到文件。请验证路径和文件名正确。”

时间:2018-06-24 11:44:21

标签: vba excel-vba outlook excel

我需要从目录中打开所有.MSG文件。

此代码有效

Sub GetMSG()
Dim objOL As Outlook.Application
Dim Msg As Outlook.MailItem

Set objOL = CreateObject("Outlook.Application")
inPath = "C:\Users\Krishna\Desktop\Test\"
MsgBox inPath
thisFile = LCase(Dir(inPath & "\*.msg"))
MsgBox thisFile
Do While thisFile <> ""

    Set Msg = objOL.Session.OpenSharedItem(inPath & thisFile)

    Msg.Display

    thisFile = Dir
Loop

下面的代码失败(我需要使用文件浏览器)

Dim Msg As Outlook.MailItem

Dim oShell As Object
Dim olApp As Object

Set olApp = CreateObject("Outlook.Application")
Set oShell = CreateObject("Shell.Application").BrowseForFolder(0, "Select Folder with attachments", 0)

If oShell Is Nothing Then MsgBox "Folder was not selected", vbCritical: Exit Sub

FldPth = oShell.self.Path

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

    Set Msg = olApp.Session.OpenSharedItem(FldPth & thisFile)

    Msg.Display

    thisFile = Dir
Loop

错误
enter image description here

当我对位置进行硬编码时,我能够打开所有文件。 Shell应用程序出现错误。

1 个答案:

答案 0 :(得分:1)

好像Set Msg = olApp.Session.OpenSharedItem(FldPth & thisFile)中缺少路径分隔符,将其添加并查看其是否有效。

相关问题