click addin button by program

S

Shinya

Can I click Addin Button by program?

exp.
Sendkey(name of addin button)

something like this?

Shinya
 
K

Ken Slovak - [MVP - Outlook]

Do you mean in the Tools, Options, Other tab, Advanced Options button, COM
Add-Ins button for Outlook? If not state the application you are working
with.

For Outlook there is no access to that dialog or its members but you can get
the Outlook.COMAddIns collection and each COMAddIn will have a Connect
Boolean property you can toggle on and off to connect and disconnect any
addin.

If that's not what you want to do then please explain.
 
S

Shinya

Probably i didnt explain clear.

I just want to click custom add in buttons by program.
I have 3 buttons in custom addin toolbar for ms word which is
developed by VB. I want to know the way to click one of buttons
in my addin toolbar by program.


Shinya
 
K

Ken Slovak - [MVP - Outlook]

If you have handles to the buttons, which you should because you are
creating them, use the button's Execute method to "click" it.
 
S

Shinya

The problem is that my button is inside of class module.Connect.cls
I have those buttons declared as class member variable

Dim WithEvents cbcMyButton As Office.CommandBarButton

and have this event.

public Sub cbcConnectButton_Click(ByVal ctrl As Office.CommandBarButton,
CancelDefault As Boolean)

how can i called this from module or other function outside of class module

Shinya
 
K

Ken Slovak - [MVP - Outlook]

You can do it a few ways. The two I prefer are to set a global button object
in a code module so it's available anywhere in your code and call that when
you want, or to set up a public method in your class that can be called from
outside the class. That public method would then call the class level button
object's Execute method. For the second way the class object would have to
be globally available.

I prefer the second method since that encapsulates the call and is a more
OOP approach.
 
S

Shinya

the first one looks simple.
In modules, I have
public g_mybutton as Office.CommandBarButton

in class module

Dim withevents mybuttonevents as office.CommandBarbutton

connect.cls( connect class for office addin )
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
...
set g_mybutton = mybuttonevents
...
end sub

public sub mybuttonevents_click(....)
msgbox "hello"
ned sub

If i set up like this how can I call this mybuttonevents_click
I tried g_mybutton.execute, but it didn't work.

Shinya
 
K

Ken Slovak - [MVP - Outlook]

You would need to instantiate the button in the class module by
instantiating an instance of that class module and then setting up the
button in the class. Then after it's all set up that way have a call in the
class that sets the class button to the global button:

' in class module
Set g_mybutton = mybuttonevents

Then anywhere in your code you can call g_mybutton.Execute to force
execution of the click event handler in the class module.
 
S

Shinya

Thanks for the post, however I am stilll having trouble with
this global button. to clarify here is my source code.

I have set this g_myButton in global module "moduleA.bas" as
Office.CommandBarButton

moduleA.bas***************************************
Public g_mybutton As Office.CommandBarButton

and have linked this to local button in class module connect.cls

"connect.cls"***************************************
'declare addin button
Dim WithEvents mybuttonevents As Office.CommandBarButton

Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'linking office addin button and global office addin button
set g_mybutton = mybuttonevents
end sub

public sub mybuttonevents_click(....)
msgbox "hello"
end sub

At the end, I have to simulate this mybuttonevents_click in moduleB.bas

"moduleB.bas"***************************************

g_myButton.Execute

I have instantiate the button in class module as well as globel moduleA.bas.
It should be working,right? Something wrong?

Shinya
 
K

Ken Slovak - [MVP - Outlook]

Are you setting the global button to the class button after you instantiate
the class button? In theory if you do that you can then call somewhere in
your code:
g_mybutton.Execute

and your button Click event in the class should fire. The click event
handler should be declared as:
private sub mybuttonevents_click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)

Are you getting any errors if you step your code?
 
S

Shinya

Here is the error message.

Runtime Error -2147467259(80004005)
Method Execute' of object '_commandBarButton' failed

Shinya
 
K

Ken Slovak - [MVP - Outlook]

It works here but I just noticed that you mentioned earlier in the thread
that you are talking about Word and not Outlook. It might be somewhat
different in Word than in Outlook.

Here's what I did in Outlook. I created a new button in On_Connect by adding
it to the Application.ActiveExplorer's Standard toolbar. Then I set the
global button to the local button, then in Startup_Complete I called the
global button's Execute method and the message box from the local button's
Click event handler was displayed.

' In code module:
Public g_button As Office.CommandBarButton

' In Designer:
Private WithEvents m_button As Office.CommandBarButton

' In On_Connection:
Dim oCB As Office.CommandBar
Set oCB = Application.ActiveExplorer.CommandBars("Standard")
Set m_button = oCB.Controls.Add(msoControlButton, , , True)
m_button.OnAction = "!<" & AddInInst.ProgId & ">"
m_button.Tag = "UniqueTag"
m_button.Caption = "Test Button"
m_button.Style = msoButtonCaption
Set g_button = m_button

' In OnStartupComplete
g_button.Execute

' The Click event handler for the button has this:
MsgBox "Test"

The g_button.Execute line of code could be anywhere in the COM addin since
the global button has visibility throughout the addin.
 
S

Shinya

Ken,

Thanks for the spending for my problem.
I found that g_button.execute works in the same class
But when you have have g_button.execute outside of this class
(saying in another module) ending up with "fail member execute
_CommandBarButton
For example you have one form class and put button on it.
I want to generate this addin click event when click on this button.
Am i trying to achieve something can not be done with VB6?

Shinya
 
S

Shinya

Ken,

I found a problem.
g_button.Execute only works in
OnStartupComplete. I added one
function in the same module and
didn't do anything not even displaying
message.

Shinya
 
K

Ken Slovak - [MVP - Outlook]

As long as the setting of the global button object is done at the time the
local button is instantiated in the designer it should be available anywhere
in your code.

I moved the call to g_button.Execute into a form in the Form_Load event and
it worked when I launched the form.

You have to make sure about scope and that your local button is in scope and
that the global button is instantiated and equated to that button. The local
button also has to remain in scope while you want access to it.
 
S

Shinya

Thanks, Ken
You have to make sure about scope and that your local button is in scope and
that the global button is instantiated and equated to that button. The local
button also has to remain in scope while you want access to it.
But I can see all the properties set correct( the same as local button) for
this
button when I step through.

Something I noticed is you create the button in
IDTExtensibility2_OnConnection
but I am creating it inside of IDTExtensibility2_OnStartupComplete. I don't
this
might cause the problem.

I will do some more research and try to find out
a way. At lease, I know there is a way to do.

Thanks,
Shinya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top