2016-09-28 93 views
0

我有幾個例程,我試圖去一起工作。前兩個找到用戶計算機上的outlook.exe文件,我希望第三個如果尚未運行,則使用該文件打開Outlook。我遇到的問題是能夠將該文件的路徑傳遞給第三個例程。這是我的代碼。任何幫助,將不勝感激。Excel VBA傳遞文件路徑到例程打開Outlook

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 
+0

什麼是找到Outlook可執行的目的是什麼?如果它安裝在機器上,所有你需要做的就是'CreateObject(「Outlook.Application」)',然後將它設置爲'.Visible'。 – Comintern

+0

也許我正在討論這個錯誤,但我認爲,因爲不同的用戶可能安裝了不同版本的Office,有些可能有32位和64位,可執行文件將位於不同的位置。我發佈的Outlook例程完美適用於我的機器,但很可能不適用於每個將使用此功能的用戶。 – DDietz

回答

1

究竟是共產國際的評論 -

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 
+0

我不得不承認我首先不是vba專家。我試過了你提供的代碼,Outlook應用程序打開幾秒鐘,然後關閉。我最初發布的Outlook例程完全按照我的想法工作,但我需要它爲32位或64位辦公室的用戶工作。也許我錯了,但我正在學習。有什麼辦法可以讓myFile作爲PATH_TO_OUTLOOK常量或類似的東西傳遞給Outlook例程嗎? – DDietz

+0

我已經更新了代碼,以便代碼完成後可以看到收件箱。要將myFile傳遞給你的Outlook例程,你需要將myFile設置爲整個模塊可見的變量(在模塊頂部聲明),或者將你的過程名更新爲Sub Outlook(PATH_TO_OUTLOOK AS String) '然後當你用'Outlook myFile'或'CALL Outlook(myFile)'調用它時(不需要調用關鍵字)。 –

+0

非常感謝您的幫助!在你的幫助下,我終於能夠按照我想要的方式工作。我確定它不是那麼精細,但它工作正常! – DDietz