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