We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

Create VB 6.0 AddIn

cybeh
cybeh asked
on
526 Views
Last Modified: 2011-09-20
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.

Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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

Author

Commented:
But will it return the wrong form if that project is having more than 1 form?

Author

Commented:
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.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.