打开目录中的文件错误“无法找到文件。验证路径和文件名是否正确。“

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打开目录中的文件错误“无法找到文件。验证路径和文件名是否正确。“相关的知识,希望对你有一定的参考价值。

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

这段代码正在运行

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

Set objOL = CreateObject("Outlook.Application")
inPath = "C:UsersKrishnaDesktopTest"
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应用程序的错误。

答案

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

以上是关于打开目录中的文件错误“无法找到文件。验证路径和文件名是否正确。“的主要内容,如果未能解决你的问题,请参考以下文章