Hi all, i'm a simple user, to imrove the office productivity i've write this vba on a MsAccess Db to download the selected table with RFC connection from Sap and write the data in Csv file (other macro import this file). After this i can use the data to send massive email to our customer with another my vba.
(I've already linked all references to Sap and Scripting in vb)
Sub sapConnection()
'
' This program connect to SAP R/3 RFC ABAP
'
'Declare the objects and variables
'
Dim functionCtrl As Object 'Function Control (Collective object)
Dim sapConnection As Object 'Connection object
Dim theFunc As Object 'Function object
'
'Create a function object
Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection'Logon with initial values
functionCtrl.LogFileName = "c:\tmp\SAPTABLEviewlog.txt"
functionCtrl.loglevel = 8
sapConnection.TraceLevel = 6
sapConnection.Client = "100"
sapConnection.User = "USERNAME" 'Replace it with the user needed for connection
sapConnection.language = "IT"
sapConnection.Password = "USER_PASSWORD"
sapConnection.System = "CHOOSE_SYTEM"
sapConnection.Destination = "CHOOSE_SYTEM"
sapConnection.Systemnumber = "00"
sapConnection.ApplicationServer = "SERVER_IP"If sapConnection.Logon(0, False) <> True Then 'Try to connect
' If no connection available
MsgBox "No connection to SAP R/3!"
Exit Sub 'End program
Else
' Connection established
MsgBox "Connected to SAP!"
' Interrogation's Variables
Dim RFC_READ_TABLE, TOPTIONS, TDATA, TFIELDS, DELIMITER As Object
Set RFC_READ_TABLE = functionCtrl.Add("RFC_READ_TABLE")
Set QUERY_TABLE = RFC_READ_TABLE.exports("QUERY_TABLE")
Set DELIMITER = RFC_READ_TABLE.exports("DELIMITER")
Set TOPTIONS = RFC_READ_TABLE.Tables("OPTIONS")
Set TDATA = RFC_READ_TABLE.Tables("DATA")
Set TFIELDS = RFC_READ_TABLE.Tables("FIELDS")
DELIMITER.Value = ";" 'Set the field separator for csv file
QUERY_TABLE.Value = "VBAP" 'The choosen table to open
' Searh options
'TOPTIONS.AppendRow
'TOPTIONS(1, "TEXT") = "MAND EQ '000'" 'Filter
'TFIELDS.AppendRow
'TFIELDS(1, "FIELDNAME") = "VBELN" 'Doc
'TFIELDS.AppendRow
'TFIELDS(1, "FIELDNAME") = "POSNR" ' Position
If RFC_READ_TABLE.Call = True Then
If TDATA.RowCount > 0 Then 'if the table is not empty
'Count rows
MsgBox "Find " + Str$(TDATA.RowCount) + " rows. Start Import."End If
Else
MsgBox "Connection RFC failed"
End If
'Make csv fileDim oFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile("C:\zrefromsap.csv")
NumRecord = TDATA.RowCount
'oFile.WriteLine "Export from SAP with VBA" 'Add a line to the file manualy
For intRow = 1 To NumRecord 'For all lines of table exec
'Insert the row in the txt
oFile.WriteLine TDATA(intRow, "WA")
intRow = intRow + 1
Next
'Close the file txt
oFile.Close
Set fso = Nothing
Set oFile = Nothing
'Clos the SAP connection
Set functionCtrl = Nothing
Set sapConnection = Nothing
MsgBox ("Export Finish!")
End IfEnd Sub
Then i've try some table but if i set QUERY_TABLE.Value to "VBAP" or "KNA1" (the 2 needed tables) The TDATA.RowCount return "0" but with 1 other table it function very well.
Can anyone explain me what's the matter? Thers a Trick?
Sorry for my terrible english.