How to customize the ribbon in VBA?

How to customize the ribbon in VBA?

Anonymous
Not applicable
2,081 Views
8 Replies
Message 1 of 9

How to customize the ribbon in VBA?

Anonymous
Not applicable

In the Default.ivb I have created a function that creates a new panel and adds some buttons in the panel.

I see the panel and the buttons, but nothing happens when I click on them.

 

The documentation says that in order to listen to the events of my buttons I should declare a "Private WithEvents ButDef As ButtonDefinition", but it when I try I get an error message "Only valid in object module"

 

Another question, still related to the events, I would expect to find a _StartUp event where I should run my function, but I can't find that event.

 

What am I missing?

 

Thanks,

Stefano

0 Likes
Accepted solutions (1)
2,082 Views
8 Replies
Replies (8)
Message 2 of 9

YuhanZhang
Autodesk
Autodesk

You should declare the events variables in a Class Module.

 

I guess what you mean the _StartUp event the ApplicationEvents.OnReady event. Hope this is what you want.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 9

Anonymous
Not applicable

Thanks, your answer helps with half the job:

- I created a new class InterfaceManager

- In the class I created one Private WithEvents ButXXX As ButtonDefinition per button

- In Class_Initialize I set the values of each ButXXX (the first time by creating them, then by picking from ThisApplication.CommandManager.ControlDefinitions)

- In Private Sub ButXXX_OnExecute the code for each button.

 

Now this function in a module initializes a global object and allows my buttons to work:

 

Public IM As New InterfaceManager
Sub StartIM()
  Set IM = New InterfaceManager
End Sub

But I still miss the second half: when Inventor starts it doesn't automatically execute my StartIM.

 

I'm looking for the equivalent of Workbook_AddinInstall in Excel.

 

I wasn't able to find _StartUp event.

What do you mean by ApplicationEvents.OnReady event?

 

Thanks,

Stefano

0 Likes
Message 4 of 9

YuhanZhang
Autodesk
Autodesk

I want to confirm that is your code in a VBA editor, or is it in an addin? Do you mean the _Startup event which should fire when load a macro or addin?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 5 of 9

Anonymous
Not applicable

I'm talking about Default.ivb, the default VBA project.

 

It has only one module, no classes. And VBA modules don't have events.

 

Thanks,

Stefano

0 Likes
Message 6 of 9

YuhanZhang
Autodesk
Autodesk

I attached a sample IVB to use the OnActivateDocument, you can follow below steps to check how it works:

 

1. New/open several document in Inventor.

2. Click Alt+F11 to open the VBA Editor.

3. File->Load Project..., choose the attached IVB to load it.

4. Double-click the Module1, and put your cursor in the ActivateDocEvent sub, and press F5 to run the macro.

5. Now activate a different document in Inventor, and you will see a dialog to inform that change.

 

You can also use F8 to debug the macro, or add break-point to the line "MsgBox DocumentObject.DisplayName & " is activated"" then you can see how it fires.

 

Hope it helps to explain how the event works.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 7 of 9

Anonymous
Not applicable

Thanks Rocky,

But this is still the first half of my answer, it doesn't answer the second half.

 

Q1) How do I manage events?

A1) (Your answer) You make a class with one or more WithEvents objects, define the event listener functions in the class, create an Init() function in a module, in the Init() function create an instance of the class, manually run that function, and from now on the events are fired in the class instance (until the next crash of the addin).

 

Q2) How do I get the events to initialize automatically when I start Inventor, in the same way as the Workbook_AddinInstall does in an Excel addin?

A2) In Excel it works because a VBA addin comes with an hidden workbook containing an instance of the ThisWorkbook object ready to manage its events; in Inventor ...?

 

I can't tell my users "hey, I made an addin for you, it has nice buttons in the ribbon, but before you can use it you need to press Alt+F8 and run the CreateRibbon macro, every time you start Inventor"

 

In your first answer you mentioned the ApplicationEvents.OnReady.

In the documentation I found the ApplicationEventsSink.OnReady.

I don't know what is the difference between these two events, but they look like (1) it's what I'm looking for, and (2) I can't use it, because inorder to use it I need to manually run a macro that initializes an object with event listeners, and by that time the OnReady event has gone.

 

Thanks,

Stefano

0 Likes
Message 8 of 9

YuhanZhang
Autodesk
Autodesk
Accepted solution
I think I understand now, you want to run the code immediately after Invnetor is launched, but you created the code as macro. I would say in Inventor the macro and addin are different, if that you want to use the code automatically after starting Inventor you should create an addin instead of a macro. Inventor could not run a macro like that of Excel(_AddinInstall), but you can specify to load an addin immediately after Inventor is launched, and in the addin you can monitor any events you need. So here I would recommend you to create an addin to implement your purpose. In the API documentary, you can find the pages for how to create an Inventor addin, and you can set the Load On Startup for the addin to let it be loaded just after Inventor is launched. If you are not working with Inventor 2012, I recommand you to create a registry-free addin, but the legacy addin also works.


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 9 of 9

Anonymous
Not applicable

So the answer to my second question "how di I create the toolbar automatically at the startup in VBA" is "you don't".

 

I guess that answers also to my other question in this thread: http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/Mouse-events-are-not-fired-when-the-co...

 

If you want to use the ribbon buttons, you need to make an addin. If you stick with VBA things might ot work, as in the example in my other thread.

 

Thanks,

Stefano