dds110
asked on
MS Word Add-In Hell - Multiple msoCommandButtons
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_C onnectMode , 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("Printba r") 'does the commandbar exist?
If pb Is Nothing Then
Err.Clear 'if we got here, it doesn't
Set pb = oWord.CommandBars.Add("Pri ntbar") '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.FindCont rol(, , pName) 'does the button already exist?
If button Is Nothing Then
Set button = oWord.CommandBars("Printba r").Contro ls.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_OnDisconnect ion(ByVal RemoveMode As AddInDesignerObjects.ext_D isconnectM ode, 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
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
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("Printba
If pb Is Nothing Then
Err.Clear 'if we got here, it doesn't
Set pb = oWord.CommandBars.Add("Pri
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.FindCont
If button Is Nothing Then
Set button = oWord.CommandBars("Printba
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_OnDisconnect
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
It's probably a silly question, but why do you want several buttons to do the same thing?
ASKER
I don't. The buttons parameter property will be read in the click event to determine which button was pressed. I have other properties that will be set to either a. display the default print dialog, or b. simply send the document to the printer. Not my idea, I'm doing this for a client.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No problem. I couldn't think of anything better.