PDA

View Full Version : Actioncontrol for short cut menus



hairywhiterabbit
05-01-2005, 07:32 AM
Hi everyone,

I have started a COM Addin project for Word and Excel. I am transferring a bunch of macros from templates and my personel.xls into this addin.

I have set up part of the custom Word toolbar and got that working using With Events. I am now working on modifying various shortcut menus to add in options like 'Insert Cross Reference', 'Insert Bookmark' and 'Paste Special'.

The way I had it in the old template was to use the ActionControl property after setting up the commandbarbutton on the shortcut menu. I have put the buttons on the respective shortcut menus OK, but the ActionControl is not working.

This is the OnConnection procedure:

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set HostApp = Application

Call wdMenu.InitCmdBarCustomizations
End Sub


The wdMenu.InitCmdBarCustomizations procedure sets the custom toolbar and then the following procedures for the short cut menus:


Option Explicit

Public HostApp As Object
Dim HostCmdBars As Office.CommandBars
Dim CBEvents As New CmdBarEvents

Private Sub MenItemsArrangement()
Dim cmdTextMenu As Office.CommandBarButton

On Error GoTo exitsubnow

Call GetCommandBars
Call wdMenu.ResetMenus

Set cmdTextMenu = HostCmdBars("text").Controls.Add(Type:=msoControlButton)
With cmdTextMenu
.Caption = "Insert Bookmark"
.FaceId = 1678
.Tag = "TextBookMark"
.BeginGroup = True
Set CBEvents.cmdTextMenu = cmdTextMenu
End With

exitsubnow:

End Sub

Sub GetCommandBars()
If HostApp.Name = "Outlook" Then
Set HostCmdBars = HostApp.ActiveExplorer.CommandBars
Else
Set HostCmdBars = HostApp.CommandBars
End If
End Sub

Sub ResetMenus()
HostCmdBars("text").Reset
End Sub

Sub DetermineMenu(ByRef sWhichDiag As String)
If sWhichDiag = "TextBookMark" Or sWhichDiag = "HelloEveryone" Then HostApp.Dialogs(wdDialogInsertBookmark).Show
End Sub


Last but not least the class module CmdBarEvents has the following code:

Public WithEvents cmdTextMenu As Office.CommandBarButton

Private Sub cmdTextMenu_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim cmdBCtl As CommandBarControl

Set cmdBCtl = HostApp.CommandBars.ActionControl
Call wdMenu.DetermineMenu(cmdBCtl.Tag)
End Sub


The trouble is that the code runs fine through Set cmdBCtl = HostApp.CommandBars.ActionControl but doesn't set the cmdBCtl. When the next line is excuted, because there is nothing there the tag can't be read.

I hope this makes sense and someone can help.

Cheers,
Andrew

Howard Kaikow
05-01-2005, 08:38 AM
My experience has been that it is easier to build an ActivX Dll that contains the macro code rather than use a COM Add-in.

hairywhiterabbit
05-02-2005, 01:21 PM
Hi Howard,

Unfortunately, I need to write the addin for Office 2000. Have you done this sort of thing with ActiveX DLLs?

Do you know if it is also possible to access a macro in a DLL from the MACROBUTTON field code?

Cheers,
Andrew

Howard Kaikow
05-02-2005, 07:37 PM
Hi Howard,

Unfortunately, I need to write the addin for Office 2000. Have you done this sort of thing with ActiveX DLLs?

Yes.


Do you know if it is also possible to access a macro in a DLL from the MACROBUTTON field code?

A stub macro is still included in the template, but all other code is in the DLLs.

For example, the following is the code for the Auto macros in my Normal.dot. All the real code is in a DLL.



Option Explicit
Public clsWordVBNormal As WordVBNormal

Public Sub AutoClose()
SetupClass
clsWordVBNormal.AutoClose
End Sub

Public Sub AutoExec()
' Runs only when global
SetupClass
clsWordVBNormal.AutoExec
End Sub

Public Sub AutoExit()
SetupClass
clsWordVBNormal.AutoExit
End Sub

Public Sub AutoNew()
SetupClass
clsWordVBNormal.AutoNew
End Sub

Public Sub AutoOpen()
SetupClass
clsWordVBNormal.AutoOpen
End Sub

Private Sub SetupClass()
If clsWordVBNormal Is Nothing Then
Set clsWordVBNormal = New WordVBNormal
clsWordVBNormal.SetClass Application
End If
End Sub