Skip to Content
0
Former Member
Aug 07, 2007 at 04:11 PM

VBA

169 Views

First let me apologize for my lack of knowledge .. you SAP folks gota have IQ's off the chart ..

in any case .. our firm is migrating to SAP .. I've been asked to investigate the creation of tools to input data that are familiar to the majority of our users eg. Excel.

I've attempted to create a macro that creates and populates a table without success. see code below .. my issue currently is that though I see the table initiated in my IDE, I can't seem to pass actual data, and I don't see the table created in the SAP environment. Though I would surely appreciate any fixes to the code offered, what I would truely appreciate is a resource to better educate me about SAP, RFC's ... ie a SAP for dummies ....

Private Declare Sub CoFreeUnusedLibraries Lib "OLE32.DLL" () ' Cleaning up memory.

Const SapSystem = "NSP"

Const SapSystemNumber = "00"

Const SapApplicationServer = "questserver"

Const SapClientNumber = "00"

Const SapUserName = "BCUSER"

Const SapUserPassword = "minisap"

Const SapLanguage = "EN"

Public sapConn As Object

Public SapFuncCall As Object

Public TableFactoryCtrl As Object

Public OurTable As Object

Public RetVal As Boolean

Public myData() As Variant

Sub Main()

SapLogon

If RetVal = False Then GoTo Bye

SapBuildObjs

SapCreateTable

SapLogOff

Bye: CleanUp

End Sub

Sub SapLogon()

Set sapConn = CreateObject("SAP.Functions")

sapConn.Connection.System = SapSystem

sapConn.Connection.SystemNumber = SapSystemNumber

sapConn.Connection.ApplicationServer = SapApplicationServer

sapConn.Connection.client = SapClientNumber

sapConn.Connection.user = SapUserName

sapConn.Connection.Password = SapUserPassword

sapConn.Connection.Language = SapLanguage

sapConn.logfilename = "LTSlog.txt"

sapConn.loglevel = 0

RetVal = sapConn.Connection.Logon(0, True)

If RetVal <> True Then

MsgBox ("Logon Failed, program will now exit")

Exit Sub

Else

MsgBox ("Logged on to " & SapApplicationServer & "/" & SapSystem & " as " & SapUserName)

End If

End Sub

Sub SapBuildObjs()

Set SapFuncCall = CreateObject("SAP.Functions")

Set TableFactoryCtrl = CreateObject("SAP.TableFactory.1")

SapFuncCall.Connection = sapConn.Connection

End Sub

Sub SapCreateTable()

Set LTSTable = TableFactoryCtrl.NewTable

Call LTSTable.Create("LTSTests", 100)

Set myRange = ActiveSheet.UsedRange

myRows = myRange.Rows.Count

myColumn = myRange.Columns.Count

For x = 1 To myColumn

LTSTable.Columns.Add

LTSTable.Columns.Item(x).Name = myRange.Cells(1, x)

Next x

ReDim myData(myColumn)

For y = 2 To myRows

Set Row = LTSTable.Rows.Add

For z = 1 To myColumn

myData(z) = myRange.Cells(y, z)

Next z

Row.Data = myData

Next y

LTSTable.Refresh

End Sub

Sub SapLogOff()

sapConn.Connection.Logoff

End Sub