我正在尝试从Excel宏VBA调用SAP函数。我可以进行连接,但每当代码到达调用函数的行时,我都会收到错误消息 “运行时错误'61704': 内部应用程序错误。
我的代码如下:
Dim functionCtrl As Object
Dim sapConnection As Object
Dim theFunc As Object
Dim PoNumber
Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection
sapConnection.System = ""
sapConnection.Client = ""
sapConnection.user = ""
sapConnection.Password = ""
sapConnection.Language = ""
If sapConnection.logon(0, False) <> True Then
MsgBox "No connection to R/3 System"
Exit Sub 'End program
End If
Set theFunc = functionCtrl.Add("BAPI_REQUISITION_CREATE")
执行最后一行时出现错误。我在引用中添加了librfc32.dll,我能够执行GUI脚本(从SAP录制)。
是否与权限或某事有关?
由于
答案 0 :(得分:1)
我不会完全回答你的问题,但我希望无论如何都能提供有用的信息。 我目前正致力于将会计数据从QuickBooks迁移到SAP。 我使用Ruby和Ruby on Rails来编写脚本和Web界面。
由于您对编写脚本感到满意,因此我建议您尝试以下操作:
安装Ruby和库非常简单。
您可能需要创建配置文件:
# c:\tmp\sapdev.yml
# adjust parameters to yours
ashost: your.sap.server.ip
sysnr: "00"
client: "100"
user: yoursaplogin
passwd: yoursappwd
lang: EN
trace: "1"
然后尝试这样的事情:
# c:\tmp\sap-test.rb
require 'rubygems'
require 'sapnwrfc'
SAPNW::Base.config_location = "c:\\tmp\\sapdev.yml"
SAPNW::Base.load_config
conn = SAPNW::Base.rfc_connect
attrib = conn.connection_attributes
# here you will find if you can connect to SAP programmatically
puts "\n>>> Connection Attributes: #{attrib.inspect}\n"
# discover the BAPI function
call = conn.discover("BAPI_ACC_DOCUMENT_CHECK").new_function_call
# set up parameters. in this case DOCUMENTHEADER, ACCOUNTGL and CURRENCYAMOUNT
call.DOCUMENTHEADER = {
"HEADER_TXT" => "EXCEL POST",
"COMP_CODE" => "2080",
"DOC_DATE" => "20090123",
"PSTNG_DATE" => "20090123",
"USERNAME" => "YOURSAPLOGIN",
"BUS_ACT" => "RFBU",
"DOC_TYPE" => "SA" # CUSTOMER INVOICE
}
puts "\n>>> DOCUMENTHEADER:"
p call.DOCUMENTHEADER
call.ACCOUNTGL = [
{"ITEMNO_ACC" => "0000000001", "GL_ACCOUNT" =>"0000500000" },
{"ITEMNO_ACC" => "0000000002", "GL_ACCOUNT" =>"0000799900", "ORDERID" => "CS_USD4110" }
]
puts "\n>>> ACCOUNTGL:"
p call.ACCOUNTGL
call.CURRENCYAMOUNT = [
{"ITEMNO_ACC" => "0000000001", "CURR_TYPE" => "00", "CURRENCY" => "USD", "AMT_DOCCUR" => "-0.70" },
{"ITEMNO_ACC" => "0000000002", "CURR_TYPE" => "00", "CURRENCY" => "USD", "AMT_DOCCUR" => "0.70" }
]
puts "\n>>> CURRENCYAMOUNT:"
p call.CURRENCYAMOUNT
call.invoke
puts "\n>>> call.RETURN:"
message = []
call.RETURN.each do |r|
message << r["MESSAGE"].strip
end
message.uniq!
puts message.join("\n")
您可能会收到有关GL帐户等的错误,但这只是我的工作示例。
这里重要的是建立RFC连接。
然后,您可以继续准备BAPI_REQUISITION_CREATE
来电,填写call.REQUISITION_ITEMS
,调用它并查看call.RETURN
。
您可能还发现Ruby和Ruby on Rails可以从Excel读取数据并与不同的数据库集成。 这对我有用。
干杯,
克塞
答案 1 :(得分:0)
要访问SAP,您至少需要s_RFC。您需要使用SAP管理部分来确定这一点,并确定您可能(或可能不需要)哪些其他权限对象。