无法从 VBA 调用 SAP BAPI 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11300503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:42:29  来源:igfitidea点击:

Unable to call SAP BAPI function from VBA

excel-vbasapbapivbaexcel

提问by caposaric

I'm trying to call SAP functions from an Excel macro, VBA. I can do the connection, but whenever the code reaches the line that calls a function, I get the error message "Run-time error '61704': Internal application error.

我正在尝试从 Excel 宏 VBA 调用 SAP 函数。我可以进行连接,但是每当代码到达调用函数的行时,我都会收到错误消息“运行时错误‘61704’:内部应用程序错误。

My code is the following:

我的代码如下:

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")

The error comes just when the last line is executed. I've added librfc32.dll in the references, I'm able to execute GUI scripts (recorded from SAP).

错误仅在执行最后一行时出现。我在引用中添加了 librfc32.dll,我能够执行 GUI 脚本(从 SAP 记录)。

Does it have something to do with permissions or something?

它与权限有关吗?

Thanks

谢谢

回答by joystick

I would not answer exactly your question but I hope to provide useful info anyway. I currently work on migration of accounting data from QuickBooks to SAP. I used Ruby and Ruby on Rails for scripting and web interface.

我不会完全回答您的问题,但无论如何我希望提供有用的信息。我目前致力于将会计数据从 QuickBooks 迁移到 SAP。我使用 Ruby 和 Ruby on Rails 来编写脚本和 Web 界面。

Since you are comfortable with scripting let me recommend to try the following:

由于您对脚本编写感到满意,因此我建议您尝试以下操作:

Installing Ruby and libraries is really simple.

安装 Ruby 和库非常简单。

You may need to create config file:

您可能需要创建配置文件:

# 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"    

Then try something like this:

然后尝试这样的事情:

# 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")

You may receive errors regarding GL accounts etc. but this is just my working example. What's important here is to get RFC connection established. Then you can move on to prepare BAPI_REQUISITION_CREATEcall, fill in call.REQUISITION_ITEMS, invoke it and check call.RETURN.

您可能会收到有关 GL 帐户等的错误,但这只是我的工作示例。这里重要的是建立 RFC 连接。然后你可以继续准备BAPI_REQUISITION_CREATE调用,填写call.REQUISITION_ITEMS,调用它并检查call.RETURN

You may also find convenient that Ruby and Ruby on Rails can read data from Excel and integrate with different databases. This is working for me.

您可能还会发现 Ruby 和 Ruby on Rails 可以从 Excel 读取数据并与不同的数据库集成,这很方便。这对我有用。

Cheers,

干杯,

Alexei

阿列克谢

回答by Richard T

To access SAP you will need at a minimun s_RFC. You need to get with your SAP admin section for this and to determine which other authority objects that you might (or might not) need.

要访问 SAP,您需要最少的 s_RFC。为此,您需要联系您的 SAP 管理部分,并确定您可能(或可能不需要)需要哪些其他权限对象。