与SAP RFC连接,电脑上必须要安装SAP软件,否则会报错ActiveX

输入工单号,无法带出SAP内接口RFC信息。

确认原因为:RFC接口需求的工单参数需要在前面加两位00,例如:1000541078(10个字符)改为 001000541078(12个字符),解决办法:使用string.PadLeft(12, "0")函数,自动补全左边两位00

RFC调用方法案例:

  Public SAPojb As Object
    Public SAPConn As Object
    Public StrSAPIP As String = ""
    Public StrSAPClient As String = ""
    Public StrSAPNo As String = ""
    Public StrSAPAcc As String = ""
    Public StrSAPPwd As String = ""
    Public ObjSAPFunc As Object
    Public theFunc As Object

    Dim R_AUFNR As String = ""
    Dim R_NUM As String = ""

 Private Sub MMF02_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadSAPpara()    '加载SAP
    End Sub

  Public Sub SAPRFCClose()    '关闭SAP
        SAPConn = Nothing
    End Sub

 Public Function LoadSAPpara() As Boolean    '加载SAP
        Try
            Dim file As String = "c:\TMIS\saprfc.ini"
            Dim Input As String = ""
            Using sr As IO.StreamReader = IO.File.OpenText(file)
                Input = sr.ReadLine()
                Input = UCase(Input)
                While sr.Peek > -1 Or Len(Input) > 0
                    If InStr(Input, "SAPIP") > 0 Then
                        StrSAPIP = Trim(Mid(Input, InStr(Input, "=") + 1))
                    End If

                    If InStr(Input, "SAPCLIENT") > 0 Then
                        StrSAPClient = Trim(Mid(Input, InStr(Input, "=") + 1))
                    End If

                    If InStr(Input, "SAPNO") > 0 Then
                        StrSAPNo = Trim(Mid(Input, InStr(Input, "=") + 1))
                    End If

                    If InStr(Input, "SAPACC") > 0 Then
                        StrSAPAcc = Trim(Mid(Input, InStr(Input, "=") + 1))
                    End If

                    If InStr(Input, "SAPPWD") > 0 Then
                        StrSAPPwd = Trim(Mid(Input, InStr(Input, "=") + 1))
                    End If

                    Input = sr.ReadLine()
                    Input = UCase(Input)
                End While
            End Using
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

 Public Function SAPRFCLink() As Boolean
        Dim NonErr As Boolean = False
        SAPojb = CreateObject("SAP.LogonControl.1")
        SAPConn = SAPojb.NewConnection
        Try
            SAPConn.applicationserver = StrSAPIP
            SAPConn.client = StrSAPClient
            SAPConn.user = StrSAPAcc
            SAPConn.password = StrSAPPwd
            SAPConn.language = "EN"
            SAPConn.CodePage = "8300"

            If SAPConn.logon(0, True) <> True Then
                NonErr = False
            Else
                NonErr = True
            End If

        Catch er As Exception
            MsgBox(er.ToString)
        End Try

        Return NonErr
    End Function

Private Sub txtWkNo_KeyDown(sender As Object, e As KeyEventArgs) Handles txtWkNo.KeyDown
        Dim str As String = ""
        Dim str2 As String = ""
        Dim RNUM As Double = 0
        R_AUFNR = txtWkNo.Text.Trim
        If e.KeyCode = Keys.Enter Then
            If SAPRFCLink() Then
                MsgBox("SAP连接成功")
                ObjSAPFunc = CreateObject("SAP.FUNCTIONS")
                ObjSAPFunc.Connection = SAPConn
                theFunc = ObjSAPFunc.add("Z_SFC_WOSMT")
                theFunc.EXPORTS("P_AUFNR") = R_AUFNR.PadLeft(12, "0")

                If theFunc.call Then
                    MsgBox("OK,RFC有回传资料")
                    str = theFunc.imports("R_AUFNR").value.ToString()
                    str2 = theFunc.imports("R_NUM").value.ToString()
                    RNUM = Convert.ToDouble(str2) - 2
                End If
            Else
                MsgBox("SAP连接错误,请检查SAP")
            End If
            lbSAP.Text = "工单:" & R_AUFNR  & ",数量:" & RNUM
            SAPRFCClose()
        End If
    End Sub

  SAPRFC.ini配置如下:

[SAPRFC]
SAPIP=10.10.10.96
SAPClient=888
SAPNo=00
SAPAcc=RFC
SAPPwd=123456

01-31 20:02