编写脚本

时间:2017-12-12 15:43:48

标签: excel-vba vba excel

我已经问了一个问题来搜索2个位置的文件然后打开它,Xbier回答了我的问题并给了我脚本但它显示编译错误我不知道如何解决这个问题,

Sub foo2()
Set fso = CreateObject("Scripting.FileSystemObject")
Set shl = CreateObject("WScript.Shell")`enter code here`

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

1 个答案:

答案 0 :(得分:0)

将代码移出ThisWorkbook的代码模块,并将其放入标准代码模块中。

ThisWorkbook.Path是只读属性,无法通过代码进行修改。

如果出于某些奇怪的原因,您需要代码成为ThisWorbook代码模块的一部分,请使用不属于{{1}的属性的变量名称对象。

相关问题