我有幾個例程,我試圖去一起工作。前兩個找到用戶計算機上的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
什麼是找到Outlook可執行的目的是什麼?如果它安裝在機器上,所有你需要做的就是'CreateObject(「Outlook.Application」)',然後將它設置爲'.Visible'。 – Comintern
也許我正在討論這個錯誤,但我認爲,因爲不同的用戶可能安裝了不同版本的Office,有些可能有32位和64位,可執行文件將位於不同的位置。我發佈的Outlook例程完美適用於我的機器,但很可能不適用於每個將使用此功能的用戶。 – DDietz