Excel VBA传递文件path以例程打开Outlook

我有几个例程,我试图去一起工作。 前两个find用户的机器上的outlook.exe文件,我想第三个使用该文件来打开Outlook,如果它尚未运行。 我遇到的问题是能够将该文件的path传递到第三个例程。 这是我的代码。 任何帮助,将不胜感激。

Sub GetSubFolders() Dim fso As New FileSystemObject Dim f As Folder, sf As Folder Dim myFile As File On Error Resume Next Set f = fso.GetFolder("C:\Program Files\") For Each myFile In f Next For Each sf In f.SubFolders Call Recursive(sf) Next Set f = fso.GetFolder("C:\Program Files (x86)\") For Each myFile In f Next For Each sf In f.SubFolders Call Recursive(sf) Next End Sub Sub Recursive(sf As Folder) Dim fso As New FileSystemObject Dim f, nsf As Folder Dim myFile As File Dim s As String Dim ofile As String On Error Resume Next Set f = fso.GetFolder(sf) For Each myFile In f.Files If Right(myFile, 11) = "outlook.exe" Then Range("A1").Value = myFile.Path Call outlook End End If Next For Each nsf In f.SubFolders Recursive nsf Next End Sub Sub outlook() Const PATH_TO_OUTLOOK = """C:\Program Files\Microsoft Office 15\root\office15\outlook.exe""" Const SHOW_MAXIMIZED = 3 Const MINIMIZE = 1 Dim oShell, oOutlook As Object On Error Resume Next Set oOutlook = GetObject(, "Outlook.Application") Set oShell = CreateObject("WScript.Shell") On Error GoTo 0 If oOutlook Is Nothing Then ' Open Outlook oShell.Run PATH_TO_OUTLOOK, SHOW_MAXIMIZED, False On Error Resume Next ' Grab a handle to the Outlook Application and minimize Set oOutlook = WScript.CreateObject("Outlook.Application") WScript.Sleep (10000) oOutlook.ActiveExplorer.WindowState = SHOW_MAXIMIZED ' Loop on error to account for slow startup in which case the ' process and/or the main Outlook window is not available Err.Clear WScript.Sleep (10000) Set oOutlook = Nothing Set oOutlook = CreateObject("Outlook.Application") oOutlook.ActiveExplorer.WindowState = MINIMIZE Set oOutlook = Nothing Set oShell = Nothing End If End Sub 

恰如共产国际评论的那样 –

 Sub Test() Dim oOL As Object Dim ns As Object Dim fldr As Object Set oOL = CreateOL Set ns = oOL.GetNameSpace("MAPI") Set fldr = ns.GetDefaultFolder(6) 'olFolderInbox fldr.display End Sub Public Function CreateOL() As Object Dim oTmpOL As Object On Error GoTo ERROR_HANDLER ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Creating an instance of Outlook is different from Excel. ' 'There can only be a single instance of Outlook running, ' 'so CreateObject will GetObject if it already exists. ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Set oTmpOL = CreateObject("Outlook.Application") Set CreateOL = oTmpOL On Error GoTo 0 Exit Function ERROR_HANDLER: Select Case Err.Number Case Else MsgBox "Error " & Err.Number & vbCr & _ " (" & Err.Description & ") in procedure CreateOL." Err.Clear End Select End Function