SAP-TABELLEN VIA EXCEL HOLEN
18.08.2004 08:41:37
Weng
ich hab ne frage:
Wie kann ich via Excel eine ganz bestimmte Tabelle von SAP in mein Excel kopieren?
Ich weis dass man es vom SAP ins Excel exportieren kann, aber ich will ja direkt vom Excel die Daten vom SAP saugen.
Wäre super wenn mir da jemand weiterhelfen kann.
Meine VBA Lösung sieht bis jetzt mal so aus(aber der bringt mir halt immernoch fehler.)
Private Sub CommandButton1_Click()
Dim fns As Object, conn As Object
Set fns = CreateObject("SAP.Functions")
Set conn = fns.Connection
With conn
.messageServer = "p10main.zeiss.org"
.Client = "010"
.Destination = "C:"
.Language = "D"
.GroupName = "P10-LOGON"
.system = "P10"
.systemid = ""
.systemnumber = 12
.user = Eingabe1.Text
.Password = Eingabe2.Text
.usesaplogonini = True
End With
If conn.Logon(0, True) <> True Then
MsgBox "Cannot logon!."
Exit Sub
End If
Dim objTableFactoryCtrl As Object
Set objTableFactoryCtrl = CreateObject("SAP.TableFactory.1")
Dim objDATA As Object
Dim objFIELDS As Object
Set objFIELDS = objTableFactoryCtrl.NewTable
boolResult = objFIELDS.CreateFromR3Repository(fns.Connection, "RFC_DB_FLD", "FIELDS")
Dim aSelFelder(2, 0)
aSelFelder(0, 0) = "feld1"
aSelFelder(1, 0) = "feld2"
aSelFelder(2, 0) = "feld3"
gobjSelFields.Data = aSelFelder
Dim objOPTIONS As Object, sOptions As String
Set objOPTIONS = objTableFactoryCtrl.NewTable
boolResult = objOPTIONS.CreateFromR3Repository(fns.Connection, "RFC_DB_OPT", "OPTIONS")
sOptions = "SETCLASS = '0101' AND SETNAME LIKE 'KS%'"
objOPTIONS.Data = sOptions
Dim Exception As Variant
Result = fns.RFC_READ_TABLE(Exception, _
QUERY_TABLE:="SETHEADERT", Data:=objDATA, _
FIELDS:=objFIELDS, Options:=objOPTIONS)
If objDATA.RowCount > 0 Then
For x = 1 To objFIELDS.RowCount
Cells(1, x) = objFIELDS(x, 1)
For y = 1 To objDATA.RowCount
Cells(1 + y, x) = Mid$(objDATA(y, 1), 1 + objFIELDS(x, 2), objFIELDS(x, 3))
Next y
Next x
End If
fns.Connection.Logoff
Set conn = Nothing
Set fns = Nothing
End Sub