Link to home
Start Free TrialLog in
Avatar of cybeh
cybeh

asked on

Create VB 6.0 AddIn

Hi,

I am trying to create a VB 6.0 Addin by using VB itself

I do have 2 problems here
1. How to get the current active project, active form?
    Set p = VBInstance.ActiveVBProject
    Set c = p.VBComponents.Item("Form1") <- I could only hardcode this, but no idea how to dynamically get the current active project active form.

2. Whenever there is a button click, it will close the form.
=========================================================
Public VBInstance As VBIDE.VBE
Public Connect As Connect

Option Explicit

Private Sub CancelButton_Click()
    Connect.Hide
End Sub


Private Sub Command1_Click()
    UpdateTag
End Sub

Private Sub Form_Load()
   Dim c As VBComponent
   Dim p As VBProject
   Dim vbc As VBControl
   Dim vbf As VBForm
   Dim sc As String
   Dim sp As String
   Dim svbc As String
   
    Set p = VBInstance.ActiveVBProject
    Set c = p.VBComponents.Item("Form1")
   
    List1.Clear
   
    If c.Type = vbext_ct_VBForm Then
        c.Activate
        Set vbf = c.Designer
        For Each vbc In vbf.VBControls
            'MsgBox vbc.Properties("name")
           List1.AddItem vbc.Properties("name")
           List1.ItemData(List1.NewIndex) = IIf(IsNumeric(vbc.Properties("tag")), vbc.Properties("tag"), 0)
        Next vbc
    End If

End Sub

Private Sub UpdateTag()
   Dim c As VBComponent
   Dim p As VBProject
   Dim vbc As VBControl
   Dim vbf As VBForm
   Dim sc As String
   Dim sp As String
   Dim svbc As String
   
    Set p = VBInstance.ActiveVBProject
    Set c = p.VBComponents.Item("Form1")
   
    If c.Type = vbext_ct_VBForm Then
        c.Activate
        Set vbf = c.Designer

        For Each vbc In vbf.VBControls
            If vbc.Properties("name") = List1.List(List1.ListIndex) Then
                vbc.Properties("tag") = Text1.Text
                Exit For
            End If
   
        Next vbc
    End If

End Sub

Private Sub List1_Click()
    Text1.Text = List1.ItemData(List1.ListIndex)
End Sub


==============================================================

When I click on the command button1, it will execute the code inside, and auto close the frmAddIn.
May I know how can I make it remain open? (I didn't write any code to hide it of close it)

Thanks.

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cybeh
cybeh

ASKER

1:
with p = vbInstance.ActiveVBProject,

p.ActiveForm <- Run Time Error 438. Object doesn't Support this property or method

Private Sub Command1_Click()
    UpdateTag
    Me.Activate <- Compile Error. Method or Data Not Found
End Sub
you can loop the components:

Set p = VBInstance.ActiveVBProject
pComponents = p.VBComponents.Count

For i = 1 To pComponents
   set c = p.VBComponents.Item(i)
    If c.Type = vbext_ct_VBForm Then
        c.Activate
        etc ...
    End If
Next

looks like I found a wrong information with ActiveForm :(


for 2: it should actually be SetFocus:

Private Sub Command1_Click()
    UpdateTag
    Me.SetFocus
End Sub

Avatar of cybeh

ASKER

But will it return the wrong form if that project is having more than 1 form?
Avatar of cybeh

ASKER

Just try out. It will give problem when there are more than 1 form, as it didn't check which is the "active" form. Please advice.
I found the problem...

Set c  = VBInstance.ActiveWindow
If c.Type = vbext_ct_VBForm Then
   c.Activate
End If


a nice blog about vb addins:
http://blog.csdn.net/cheneychao/archive/2006/03/20/630084.aspx