我有几个例程,我试图去一起工作。前两个找到用户计算机上的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