繁体   English   中英

执行 SAP RFC 的 VBA 程序在 Excel 2016 on-prem 中有效,但在 Office 365 中无效(运行时错误 20080008…Bad Variant Type)

[英]VBA program executing SAP RFCs works in Excel 2016 on-prem, but not in Office 365 (Run-time error 20080008…Bad Variant Type)

首先让我说,我在这里这里看到了提到这个问题的其他两个线程,但它们并没有帮助我解决我的问题。

我已经在本地 Excel 2016 环境(32 位)中测试了几个星期的程序,没有任何问题。 我的公司很快就会迁移到 Office 365,所以我决定也在那里进行测试。 在该系统上,我在Functions.Connection = objConnection行上遇到运行时错误

Option Explicit
Public Functions As SAPFunctionsOCX.SAPFunctions
Private LogonControl As SAPLogonCtrl.SAPLogonControl
Private objConnection As SAPLogonCtrl.Connection
Public Func As SAPFunctionsOCX.Function
Public Commit As SAPFunctionsOCX.Function
Public TableFactory As SAPTableFactory
Public silentLogon As Boolean
Public tblReadTableOptions, tblReadTableFields, tblReadTableData As SAPTableFactoryCtrl.Table

Sub ExtractProjectData()
    If objConnection Is Nothing Then LogonToSAP
    InitiateSAPVariables
    Set Func = Functions.Add("BBP_RFC_READ_TABLE")
    Set tblReadTableOptions = Func.Tables("OPTIONS")
    Set tblReadTableFields = Func.Tables("FIELDS")
    Set tblReadTableData = Func.Tables("DATA")

    'extract/transform data from SAP tables

End Sub

Function InitiateSAPVariables()
    Set Functions = Nothing
    Set TableFactory = Nothing
    Set Func = Nothing
    Set Functions = CreateObject("SAP.Functions")
    Set TableFactory = CreateObject("SAP.TableFactory.1")
    Functions.Connection = objConnection 'run-time error here in Office 365 but not in on-prem
End Function

Function LogonToSAP()
    Dim establishConnection As Boolean
    silentLogon = false
    Set LogonControl = CreateObject("SAP.LogonControl.1")
    Set objConnection = LogonControl.NewConnection
    objConnection.Client = "###"
    objConnection.Language = "EN"
    objConnection.SystemNumber = "##"
    objConnection.User = ""
    objConnection.Password = ""
    objConnection.HostName = "###############"
    objConnection.System = "###"
    objConnection.ApplicationServer = "###.###.#.##"
    establishConnection = objConnection.Logon(0, silentLogon)
End Function

快速检查 objConnection 告诉我登录成功......所以我知道该部分正在 365 上工作。但由于某种原因,它不喜欢在 365 环境中分配Functions SAPFunctionsOCX.SAPFunctions object 的Connection属性(请随时纠正我的措辞......我知道这不太正确)。

请注意,我没有看到任何参考问题,也没有在任一环境中遇到任何编译错误。 麻烦的第一个迹象是执行Functions.Connection = objConnection

There's one more twist here and that is that I have another older VBA program that logs into SAP and runs remote function calls that doesn't use SAPFunctionsOCX.SAPFunctions, but rather declares variable R3 as Public R3 As Object and then sets R3 later in the登录代码为Set R3 = CreateObject("SAP.Functions") ...它不使用 OCX。 换句话说,旧例程使用后期绑定。 当以这种方式设置功能 object(在本例中为 R3)时,我能够在 prem 和 Office 365 环境中运行 RFC。

Function LogonProdSAP(Optional SuppressLoginScreen As Boolean)
    Application.ScreenUpdating = False
    '**********************************************
    'Create Server object and Setup the connection for DEV
    '**********************************************
    Set R3 = CreateObject("SAP.Functions")
    If SuppressLoginScreen Then
        R3.Connection.System = "###"
        R3.Connection.HostName = "###################"
        R3.Connection.SystemNumber = "##"
        R3.Connection.Client = "###"
        R3.Connection.User = "##########"
        R3.Connection.Password = "#########"
        R3.Connection.Language = "EN"
'        Call Logger("LogonProdSAP>  " & GetUserName)
    End If
    LogonProdSAP = R3.Connection.logon(0, SuppressLoginScreen)
    If LogonProdSAP <> True Then MsgBox ("Logon error"): Exit Function
End Function

我可以只是 go 回到这样做,但我宁愿不必重新配置我刚刚设置的所有代码。 此外,我更喜欢尽早绑定,因此 Intellitype 可以显示 object 可用的所有属性/方法。 我敢肯定还有其他好处。

我必须做些什么才能让早期绑定技术在 Office 365 上运行?

这是因为您的 Office 是 64 位版本,并且 Windows 版本 7.60 的 SAP GUI 是 32 位(下一个 SAP GUI 版本 7.70 应该是 64 位,所以它应该再次工作)。

You have a workaround to make VBA work with SAP GUI 32-bits DLL, by using DLL Surrogate , ie by editing the Windows Registry of all incompatible SAP GUI DLL. 最初的解决方案是在 SAP Community 中提出的

为了简化任务,您可以创建my.REG 文件,执行它以自动更新 Windows 注册表,然后您的 VBA 宏应该可以工作。

我在这里复制 my.REG 文件:

; ====================================================================================
; SAP Logon Unicode Control %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtlogU.ocx {0AAF5A11-8C04-4385-A925-0B62F6632BEC}
; ====================================================================================

[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]
"DllSurrogate"=""

[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]
"AppID"="{0AAF5A11-8C04-4385-A925-0B62F6632BEC}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{0AAF5A11-8C04-4385-A925-0B62F6632BEC}]

; ====================================================================================
; SAP Remote Function Call Unicode Control %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtfuncu.ocx {0AF427E7-03B9-4673-8F21-F33A683BCE28}
; ====================================================================================

[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]
"DllSurrogate"=""

[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]
"AppID"="{0AF427E7-03B9-4673-8F21-F33A683BCE28}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{0AF427E7-03B9-4673-8F21-F33A683BCE28}]

; ====================================================================================
; SAP Logon Control (not Unicode) %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtlog.ocx {B24944D6-1501-11CF-8981-0000E8A49FA0}
; ====================================================================================

[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]
"DllSurrogate"=""

[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]
"AppID"="{B24944D6-1501-11CF-8981-0000E8A49FA0}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{B24944D6-1501-11CF-8981-0000E8A49FA0}]

; ====================================================================================
; SAP Remote Function Call Control (not Unicode) %ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\wdtfuncs.ocx {5B076C03-2F26-11CF-9AE5-0800096E19F4}
; ====================================================================================

[HKEY_CLASSES_ROOT\WOW6432Node\AppID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]
"DllSurrogate"=""

[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]
"AppID"="{5B076C03-2F26-11CF-9AE5-0800096E19F4}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{5B076C03-2F26-11CF-9AE5-0800096E19F4}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM