Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling RFC funtions from VBA

Former Member
0 Kudos

Hi all,

I would like to call RFC funtions from VBA, could you please advise on what library should I set reference to?

A piece of sample code would also be highly appreciated.

Thanks in advance.

Tomas

1 ACCEPTED SOLUTION

athavanraja
Active Contributor
0 Kudos

here is a small code sample using vbscript

<html> 
<head> 
<meta http-equiv="Content-Language" content="en-us">
 
<title>RFC Example - THUSRINFO</title> 

<script language="VBScript"> 
sub get_cust_data() 

dim fns 
set fns = createobject("SAP.Functions") 
fns.logfilename = "my_log.txt" 
fns.loglevel = 6 

dim conn 
set conn = fns.connection

 

conn.ApplicationServer = "application server name or ip address" 
conn.System = "DEV" 
conn.user = document.myf.t1.value 
conn.password = document.myf.t2.value 
conn.Client = "025" 
conn.Language = "E" 
conn.tracelevel = 6 

if conn.logon(0, true) <> true then 
exit sub 
end if 


dim myfunct 
set myfunc = fns.add("THUSRINFO") 


result = myfunc.Call 
the_exception = myfunc.exception 

dim users 
if result = true then 
set users = myfunc.tables.item("USR_TABL") 
document.write "Table USR_TABL, " & users.rowcount & " rows" 
document.write "<table border=1>" 
document.write "<tr>" 
document.write "<td>BNAME</td>" 
document.write "<td>MANDT</td>" 
document.write "<td>TCODE</td>" 
document.write "<td>TERM</td>" 
document.write "<td>ZEIT</td>" 
document.write "<td>HOSTADR</td>" 
document.write "<td>RFC TYPE</td>" 
document.write "</tr>" 
for each user in users.Rows 
document.write "<tr>" 
document.write "<tr>" 
document.write "<td>" 
document.write trim(user("BNAME")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("MANDT")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("TCODE")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("TERM")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("ZEIT")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("HOSTADR")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("RFC_TYPE")) 
document.write "</td>" 
next 
document.write "</table>" 
set users = nothing 
end if 



fns.connection.logoff 
Set fns = nothing 
Set conn = nothing 

end sub 
</script> 
</head> 

<body> 

<form method="POST" name="myf">
  User Id <input type="text" name="T1" size="20"></p>
  <p>Password<input type="password" name="T2" size="20"></p>
  <p> </p>
  <p><input type="button" value="Submit" onclick="get_cust_data()" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body> 

</html>

Regards

Raja

2 REPLIES 2

former_member188685
Active Contributor
0 Kudos

Hi,

check this thread you can see lot of links..

Regards

vijay

athavanraja
Active Contributor
0 Kudos

here is a small code sample using vbscript

<html> 
<head> 
<meta http-equiv="Content-Language" content="en-us">
 
<title>RFC Example - THUSRINFO</title> 

<script language="VBScript"> 
sub get_cust_data() 

dim fns 
set fns = createobject("SAP.Functions") 
fns.logfilename = "my_log.txt" 
fns.loglevel = 6 

dim conn 
set conn = fns.connection

 

conn.ApplicationServer = "application server name or ip address" 
conn.System = "DEV" 
conn.user = document.myf.t1.value 
conn.password = document.myf.t2.value 
conn.Client = "025" 
conn.Language = "E" 
conn.tracelevel = 6 

if conn.logon(0, true) <> true then 
exit sub 
end if 


dim myfunct 
set myfunc = fns.add("THUSRINFO") 


result = myfunc.Call 
the_exception = myfunc.exception 

dim users 
if result = true then 
set users = myfunc.tables.item("USR_TABL") 
document.write "Table USR_TABL, " & users.rowcount & " rows" 
document.write "<table border=1>" 
document.write "<tr>" 
document.write "<td>BNAME</td>" 
document.write "<td>MANDT</td>" 
document.write "<td>TCODE</td>" 
document.write "<td>TERM</td>" 
document.write "<td>ZEIT</td>" 
document.write "<td>HOSTADR</td>" 
document.write "<td>RFC TYPE</td>" 
document.write "</tr>" 
for each user in users.Rows 
document.write "<tr>" 
document.write "<tr>" 
document.write "<td>" 
document.write trim(user("BNAME")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("MANDT")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("TCODE")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("TERM")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("ZEIT")) 
document.write "</td>" 
document.write "<td>" 
document.write trim(user("HOSTADR")) 
document.write "</td>" 
document.write "<td> " 
document.write trim(user("RFC_TYPE")) 
document.write "</td>" 
next 
document.write "</table>" 
set users = nothing 
end if 



fns.connection.logoff 
Set fns = nothing 
Set conn = nothing 

end sub 
</script> 
</head> 

<body> 

<form method="POST" name="myf">
  User Id <input type="text" name="T1" size="20"></p>
  <p>Password<input type="password" name="T2" size="20"></p>
  <p> </p>
  <p><input type="button" value="Submit" onclick="get_cust_data()" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body> 

</html>

Regards

Raja