We help IT Professionals succeed at work.
Get Started

MS Word Add-In Hell - Multiple msoCommandButtons

485 Views
Last Modified: 2013-12-25
THIS IS A VB6 ADD-IN FOR MS WORD!!!!

I've been trying to figure this out for several hours now to no avail.  What I'm trying to do is add a button to a new CommandBar for each printer attached to my system.  Each button will have the same .OnAction.  What's happening is only the last button created is getting the .OnAction property assigned (so I know it works).  If the buttons weren't Office CommandBarButtons, I could create a button array, but NNNNOOOOOOOOOOOOO, Microsoft was having none of that.

So the basic question is this:  How do I assign a new button to the commandbar and assigne each button to the same OnAction event?  Here's my code!!!  (forgive me for posting all of it).

Option Explicit
Public oWord As Object
Public WithEvents button As Office.CommandBarButton


Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error Resume Next
Set oWord = Application
Dim pb As Object                            'new command bar
Dim p As Printer                            'printer object
Dim pName As String                         'printer name without the path

Set pb = oWord.CommandBars("Printbar")      'does the commandbar exist?
If pb Is Nothing Then
  Err.Clear                                 'if we got here, it doesn't
  Set pb = oWord.CommandBars.Add("Printbar") 'create it
  pb.Visible = True                         'let's see it
End If

For Each p In Printers                                              'create a new button for each printer in the printers collection
  pName = Mid(p.DeviceName, InStrRev(p.DeviceName, "\") + 1)
  Set button = oWord.CommandBars.FindControl(, , pName)             'does the button already exist?
  If button Is Nothing Then
    Set button = oWord.CommandBars("Printbar").Controls.Add(1)      'no? Let's create it
    button.Tag = pName
    button.Parameter = pName
    button.Caption = pName
    button.Style = msoButtonCaption
    button.OnAction = "!<" & AddInInst.ProgId & ">"                 'this is what we do when we click it!
    button.Visible = True                                           'gotta see it to click it
  End If
Next p

End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
Set button = Nothing
Set oWord = Nothing
End Sub

Private Sub button_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox Ctrl.Parameter           'msgbox to see if it's working
End Sub
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE