title: Connect a third-party application (MS Excel) with SAP R/3
body: <p>I'm working out a solution for communicate MS Excel (a VBA macro) with SAP. Sometimes is too hard to reach SAP support people at any company you are working for, so it is better to figure out your own solution. </p>
  
  <p>About that reason, I started with the basic: "connect my own User profile with SAP R/3". Doing some Google research, I found a good <a href="http://scn.sap.com/docs/DOC-10447" rel="nofollow">paper</a> with the connectivity and here is the key part of the code:</p>
  
  <pre><code>Dim objBAPIControl As Object 'Function Control (Collective object)
  Dim sapConnection As Object 'Connection object
  Set objBAPIControl = CreateObject("SAP.Functions")
  Set sapConnection = objBAPIControl.Connection
  
  sapConnection.client = "32"
  sapConnection.user = "myUser"
  sapConnection.Language = "EN"
  sapConnection.hostname = "qwerty.example.com"
  sapConnection.Password = "myPass"
  </code></pre>
  
  <p>(and more user profile values........)</p>
  
  <p>Later on, I did the second step which was reading some table (in this case, the Users Table)</p>
  
  <pre><code>If sapConnection.logon(1, True) &lt;&gt; True Then
  MsgBox "No connection to R/3!"
  Exit Sub 'End program
  End If
  Set objUserList = objBAPIControl.Add("BAPI_USER_GETLIST")
  Set objUserDetail = objBAPIControl.Add("BAPI_USER_GET_DETAIL")
  
  returnFunc = objUserList.Call
  If returnFunc = True Then
  Dim objTable As Object
  Set objTable = objUserList.Tables("USERLIST")
  ActiveSheet.Cells(1, 1) = "User count :" &amp; objTable.RowCount
  </code></pre>
  
  <p>But now, here is my question: How to run some transaction (LM02, LS26, LX03, or etc) fired from VBA ?</p>
  
  <h2>Thank you guys!</h2>
  
  <p>PS. I'm using MS Office 2007 and Windows 7.</p>
  
  <p>--
  EDITED:</p>
  
  <p>Hey I change the way I was attacking the challenge ( I will let the OP above for helping some other guy)</p>
  
  <p>This must be add to the first</p>
  
  <pre><code>Dim RfcCallTransaction As Object
  Dim Messages As Object
  Dim BdcTable As Object
  </code></pre>
  
  <p>The connection to SAP is the same, but once you're logged in: </p>
  
  <pre><code>If objBAPIControl.Connection.Logon(0, False) &lt;&gt; True Then
      Exit Sub
  End If
  
  Set RfcCallTransaction = objBAPIControl.Add("RFC_CALL_TRANSACTION_USING") 
  </code></pre>
  
  <p><a href="http://www.se80.co.uk/sapfms/r/rfc_/rfc_call_transaction_using.htm" rel="nofollow">here</a> you can find the "new" arguments for the <code>RFC_CALL_TRANSACTION</code> old function.</p>
  
  <pre><code>    RfcCallTransaction.exports("tcode") = "SE16"
      RfcCallTransaction.exports("mode") = "N"
  Set BdcTable = RfcCallTransaction.Tables("bt_data")
  </code></pre>
  
  <p>Until this part I'm sure of three things:</p>
  
  <p>1) It does connect to SAP R/3</p>
  
  <p>2) It Runs the "SE16" transaction</p>
  
  <p>3) It could receive the Batch input from a BdcTable</p>
  
  <p>The missing part is, how to "format" the BdcTable (I think it must be in ABAP language) to upload the exact Data (table) that I want to be run at SE16.</p>
  
  <p>EDITED:
  I found an example of the "formatting" table I'm looking for:</p>
  
  <pre><code>add_bdcdata BdcTable, "SAPLSETB", "230", "X", "", ""
  add_bdcdata BdcTable, "", "", "", "BDC_CURSOR", "DATABROWSE-TABLENAME"
  add_bdcdata BdcTable, "", "", "", "BDC_OKCODE", "=ANZE"
  add_bdcdata BdcTable, "", "", "", "DATABROWSE-TABLENAME", "KNA1"
  add_bdcdata BdcTable, "/1BCDWB/DBKNA1", "1000", "X", "", ""
  add_bdcdata BdcTable, "", "", "", "BDC_CURSOR", "MAX_SEL"
  add_bdcdata BdcTable, "", "", "", "BDC_OKCODE", "=ONLI"
  add_bdcdata BdcTable, "", "", "", "LIST_BRE", "250"
  add_bdcdata BdcTable, "", "", "", "MAX_SEL", "5"
  add_bdcdata BdcTable, "SAPMSSY0", "120", "X", "", ""
  add_bdcdata BdcTable, "", "", "", "BDC_CURSOR", "01/02/2012"
  add_bdcdata BdcTable, "", "", "", "BDC_OKCODE", "=%EX"
  add_bdcdata BdcTable, "/1BCDWB/DBKNA1", "1000", "X", "", ""
  add_bdcdata BdcTable, "", "", "", "BDC_OKCODE", "/EE"
  add_bdcdata BdcTable, "", "", "", "BDC_CURSOR", "I1-LOW"
  add_bdcdata BdcTable, "SAPLSETB", "230", "X", "", ""
  add_bdcdata BdcTable, "", "", "", "BDC_OKCODE", "/EBACK"
  add_bdcdata BdcTable, "", "", "", "BDC_CURSOR", "DATABROWSE-TABLENAME"
  </code></pre>
  
  <p>The above code still being part of the VBA macro. But I don't know what do all those fills mean.</p>
  
  <p>After this task is complete, the second challenge is to save the data automatically into a MS Excel sheet.</p>
  
  <p>(Could you tell me if the all editing thing is working or should I just paste the new code and erase the history?)</p>
  
  <p>EDITED:
  I asked the same question as a summary in SCN platform. You can review it <a href="http://scn.sap.com/thread/3236236" rel="nofollow">here</a>.</p>
  
  <p>EDITED:
  My objective in this challenge is to run a transaction ( the TCODE I'm looking for is LM02, a bin to bin transaction for warehouse) via a VBA Macro, upload some date extracted from a MS Excel file and Execute the transaction.</p>
  
  <p>--
  last edit:
  It seems like this task can't be done without the help of SAP IT support.
  19/sep/12 | still researching about this topic. 25/09/12 |
  02/OCT/12 --> I will try <a href="http://www.autoitscript.com/site/autoit/" rel="nofollow">AutoIT</a> for logging, run a basic Tcode and display the report. I'd Comment my results after I double checked it.</p>