Skip to Content
0
Former Member
Feb 07, 2005 at 01:30 AM

ActiveX Problem

240 Views

Hi all,

I have some trouble to use the Microsoft Tree Active X.

I'm using VB.Net

Business One 2004A (6.7)

And I add the following COM reference :

C:\WINDOWS\system32\MSCOMCTL.OCX

I think this ocx contains ATL Active X (the only kind supported by Business One)

Below is my code, the error (System.Runtime.InteropServices.COMException) appears at the line AcXTree.ClassID = "MScomctl.TreeCtrl.2" (in Bold in the code)

The error just says :Exception from HRESULT: 0xFFFFFFFF.

I can't find where the error come from.

Thanks for your help.

Module to start the Add On

Module SubMain
    Public Sub Main()
        Dim oActiveXTree As ActiveXTree
        oActiveXTree = New ActiveXTree
        Windows.Forms.Application.Run()
    End Sub
End Module

Here is the class to create the form with ActiveX

Option Strict Off
Option Explicit On 

Friend Class ActiveXTree

    Private WithEvents SBO_Application As SAPbouiCOM.Application
    Private oCompany As SAPbobsCOM.Company
    Private WithEvents oTreeView As mscomctl.TreeView
    Private oEdit As SAPbouiCOM.EditText


    Public Sub New()
        MyBase.New()
        Class_Initialize_Renamed()
    End Sub


    Private Sub Class_Initialize_Renamed()

        SetApplication()

        If Not SetConnectionContext() = 0 Then
            SBO_Application.MessageBox("Failed setting a connection to DI API")
            End ' Terminating the Add-On Application
        End If

        If Not ConnectToCompany() = 0 Then
            SBO_Application.MessageBox("Failed connecting to the company's Data Base")
            End ' Terminating the Add-On Application
        End If
        SBO_Application.MessageBox("DI Connected To: " & oCompany.CompanyName & vbNewLine & "Hello World!")
        CreateFormWithActiveX()
    End Sub

    Private Sub SetApplication()

        Dim SboGuiApi As SAPbouiCOM.SboGuiApi
        Dim sConnectionString As String

        SboGuiApi = New SAPbouiCOM.SboGuiApi

#If DEBUG Then
        sConnectionString = "0030002C0030002C00530041005000420044005F00440061007400650076002C0050004C006F006D0056004900490056"
#Else
        sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
#End If

        SboGuiApi.Connect(sConnectionString)

        SBO_Application = SboGuiApi.GetApplication()

    End Sub

    Private Function SetConnectionContext() As Integer

        Dim sCookie As String
        Dim sConnectionContext As String
        Dim lRetCode As Integer

        oCompany = New SAPbobsCOM.Company
        sCookie = oCompany.GetContextCookie
        sConnectionContext = SBO_Application.Company.GetConnectionContext(sCookie)
        If oCompany.Connected = True Then
            oCompany.Disconnect()
        End If
        SetConnectionContext = oCompany.SetSboLoginContext(sConnectionContext)

    End Function

    Private Function ConnectToCompany() As Integer

        '// Establish the connection to the company database.
        ConnectToCompany = oCompany.Connect

    End Function

    Private Sub CreateFormWithActiveX()
        Dim CP As SAPbouiCOM.FormCreationParams
        Dim fTree As SAPbouiCOM.Form
        Dim AcXTree As SAPbouiCOM.ActiveX
        Dim oItem As SAPbouiCOM.Item
        Dim oStatic As SAPbouiCOM.StaticText
        Dim oLink As SAPbouiCOM.LinkedButton

        ' Set the form creation parameters
        CP = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
        CP.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_Fixed
        CP.FormType = "ACXTree"
        CP.UniqueID = "ACTree1"

        ' Add a new form
        fTree = SBO_Application.Forms.AddEx(CP)
        fTree.Left = 100
        fTree.Top = 100
        fTree.Width = 300
        fTree.Height = 300
        fTree.Height = 300
        fTree.Title = "TreeView - ActiveX"
        fTree.Visible = True

        ' Add the edit text and linked button
        oItem = fTree.Items.Add("staticBP", SAPbouiCOM.BoFormItemTypes.it_STATIC)
        oItem.Left = 20
        oItem.Top = 10
        oItem.Width = 200
        oStatic = oItem.Specific
        oStatic.Caption = "Business Parnters with contacts"

        ' Add the edit text and linked button
        oItem = fTree.Items.Add("txtBP", SAPbouiCOM.BoFormItemTypes.it_EDIT)
        oItem.Left = 20
        oItem.Top = 250
        oItem.Width = 60
        oEdit = oItem.Specific

        oItem = fTree.Items.Add("BPLink", SAPbouiCOM.BoFormItemTypes.it_LINKED_BUTTON)
        oItem.Left = 80
        oItem.Top = 250
        oItem.LinkTo = "txtBP"
        oLink = oItem.Specific
        oLink.LinkedObject = SAPbouiCOM.BoLinkedObject.lf_BusinessPartner

        ' Add the TreeView Control to the form
        oItem = fTree.Items.Add("Tree", SAPbouiCOM.BoFormItemTypes.it_ACTIVE_X)
        oItem.Left = 20
        oItem.Top = 40
        oItem.Width = 250
        oItem.Height = 200

        ' Create the new activeX control
        AcXTree = oItem.Specific
        AcXTree.ClassID = <b><u>"MScomctllib.TreeCtrl.2"</u></b> ' HERE IS MY ERROR
        oTreeView = AcXTree.Object

        'Loading date to Tree Nodes
        Dim BPNode As mscomctl.Node
        Dim ContactNode As mscomctl.Node
        Dim oRecordSet As SAPbobsCOM.Recordset
        Dim oBPs As SAPbobsCOM.BusinessPartners
        Dim iContact As Integer

        ' Reference all the Business Partners
        oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet.DoQuery("SELECT * FROM OCRD")
        oBPs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)
        oBPs.Browser.Recordset = oRecordSet

        ' Iterate all the BPs
        oBPs.Browser.MoveFirst()
        While oBPs.Browser.EoF = False
            BPNode = oTreeView.Nodes.Add(, , "B" & oBPs.CardCode, oBPs.CardName)
            For iContact = 0 To oBPs.ContactEmployees.Count() - 1
                oBPs.ContactEmployees.SetCurrentLine(iContact)
                If oBPs.ContactEmployees.Name <> "" Then
                    ContactNode = oTreeView.Nodes.Add(, , "C" & oBPs.CardCode & iContact.ToString, oBPs.ContactEmployees.Name)
                    ContactNode.Parent = BPNode
                End If
            Next iContact
            oBPs.Browser.MoveNext()
        End While
        ' Make the form visible
        fTree.Visible = True

    End Sub

    Private Sub oTreeView_NodeClick(ByVal Node As mscomctl.Node) Handles oTreeView.NodeClick
        If oTreeView.SelectedItem.Key.StartsWith("B") Then
            oEdit.Value = oTreeView.SelectedItem.Key.TrimStart("B")
        End If
    End Sub
End Class