用于在2个位置搜索和打开文件的VBA代码

时间:2017-12-11 17:26:29

标签: excel-vba vba excel

我有一个宏来打开一个SAP文件(C:\ Program Files(x86)\ SAP \ FrontEnd \ SAPgui \ saplogon.exe)我还想包含路径(C:\ Program Files \ SAP \ FrontEnd \ SAPgui \ saplogon.exe)在相同的代码中搜索任一位置并打开它

代码是:

Private Sub Workbook_Open()

Dim sapConn As Object

Set objshell = CreateObject("WScript.Shell")
Set objapp = objshell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

        sapConn.Connection.ApplicationServer = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
        sapConn.Connection.Client = "100" ' only read access


End Sub

1 个答案:

答案 0 :(得分:0)

如何在运行程序之前检查文件夹是否存在:

Sub foo2()
Set fso = CreateObject("Scripting.FileSystemObject")
Set shl = CreateObject("WScript.Shell")

Path = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\" 'path to folder
exists = fso.FolderExists(Path)

If (exists) Then
    program = "saplogon.exe" 'Program name to run
    shl.Run (Path & program) 'Run a program
Else
    Path = "C:\Program Files\SAP\FrontEnd\SAPgui\"
    program = "saplogon.exe" 'Program name to run
    shl.Run (Path & program) 'Run a program

End If
    Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object
    sapConn.Connection.ApplicationServer = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
    sapConn.Connection.Client = "100" ' only read access
End Sub