|
-
Jul 4th, 2005, 05:22 AM
#1
How do I delete a command bar button from a word addin
I wasn't sure if this should be under the vba section or the com activeX section (or possibly classic VB) so if I've put it in the wrong place please acould a mod move it for me and accept my apologies
I'm writing an addin for word and need to put a button on the command bar. I cribbed the following code:-
Code:
Dim oWord As Object
Dim WithEvents cmdBrowseSLXContacts As Office.CommandBarButton
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set oWord = Application
Set cmdBrowseSLXContacts = oWord.CommandBars("Standard").Controls.Add(1)
With cmdBrowseSLXContacts
.Caption = "Browse SLX Contacts"
.Style = msoButtonCaption
.Tag = "Browse SLX Contacts"
.OnAction = "!<" & AddInInst.ProgId & ">"
.Visible = True
End With
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
cmdBrowseSLXContacts.Delete
Set cmdBrowseSLXContacts = Nothing
Set oWord = Nothing
End Sub
from the microsoft help files.
The trouble is the example in the link was for an excel plugin and there appears to be a slight difference in the behaviour when I come to delete the button in the OnDisconnection event. It works fine in excel but when I try it in word I get a 'Run time error 424 : Object required' msgbox and the button doesn't delete (ie, the next time I open word there's 2 buttons, then 3, then 4, then I get frustrated and remove them all manually )
I think that the reference to cmdBrowseSLXContacts has been lost somewhere before the disconnect method is called (it exists when word is running because the events on it fire just fine) so when the cmdBrowseSLXContacts.delete call is made there's no cmdBrowseSLXContacts for it to act against. Can anyone help me with how to do this?
-
Jul 4th, 2005, 09:15 AM
#2
Re: How do I delete a command bar button from a word addin [resolved (kinda')]
hmmm... stranger and stranger.
I tried doing a findcontrol to make sure cmdBrowseSLXContacts was still pointing at the actual button. I no longer get the 'object required' error and I can message box out the buttons caption so I'm definitely pointing to the correct control now. Trouble is that the button still doesn't actually get removed. The only conclusion I could draw from this is that the delete method isn't working, so...
I tried moving the delete line into the OnConnection method, just after the controls.add line. I put a message box between them just to temporarily halt execution. Now you can see the button get created and then removed correctly. So the delete method DOES work. I am bamboozled
Anyway, I came up with a work around (actually my colleagues suggestion but I'll take the credit anyway) that I thought I'd post in case someone else gets stuck with this. Instead of deleting the button in the onDisconnect event I simply check for itr's existence before adding it in the onConnection event. So the code becomes :-
Code:
Option Explicit
Dim oWord As word.Application
Dim WithEvents cmdBrowseSLXContacts As Office.CommandBarButton
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set oWord = Application
Set cmdBrowseSLXContacts = oWord.CommandBars("Standard").FindControl(1, , "Browse SLX Contacts")
If cmdBrowseSLXContacts Is Nothing Then
Set cmdBrowseSLXContacts = oWord.CommandBars("Standard").Controls.Add(1)
With cmdBrowseSLXContacts
.Caption = "Browse SLX Contacts"
.Style = msoButtonCaption
.Tag = "Browse SLX Contacts"
.OnAction = "!<" & AddInInst.ProgId & ">"
.Visible = True
End With
End If
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
Set cmdBrowseSLXContacts = Nothing
Set oWord = Nothing
End Sub
This works just fine
-
Jul 4th, 2005, 10:19 AM
#3
Re: How do I delete a command bar button from a word addin
You could also create it and make it invisible (or disabled). Other alternatives. Congrats on finding an answer!
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 4th, 2005, 10:21 AM
#4
Re: How do I delete a command bar button from a word addin
Although they both have a "Standard" commandbar, they are different bars. Where are you wanting the button to be placed?
Excel has a "Worksheet menu bar" as its first item and Word has a "Standard" menu bar as its first item.
Its always best to use ".FindControl" before adding an item.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 4th, 2005, 11:39 AM
#5
Re: How do I delete a command bar button from a word addin
Vince - wouldn't that still leave me with the problem of the buttons not being removed though? Sure they wouldn't be visible but they'd still be there and eventually this'd have to cause some kind of overflow.
Rob - To be honest I'm not bothered where it appears, just so long as it does appear, and then doesn't appear twice the next time you open word. The menubar it added to with "standard" looked like an apropriate spot though.
I'm not sure what you mean by using findControl before adding the button. Could you post an example for me to try out?
Although I got around the issue I'd still like to find out exactly what the problem is so any more info is gratefully received.
Thanks as always to both of you.
-
Jul 4th, 2005, 11:44 AM
#6
Re: How do I delete a command bar button from a word addin
This is the proper way to test if a commandbar button exists already.
VB Code:
Set cmdBrowseSLXContacts = oWord.CommandBars("Standard").FindControl(1, , "Browse SLX Contacts")
If cmdBrowseSLXContacts Is Nothing Then
I am unsure of what your actual issue is if you already are testing for the button?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 4th, 2005, 11:49 AM
#7
Re: How do I delete a command bar button from a word addin
Just re-read the thread a couple of times and it looks like you just want to remove the button on the disconnection of the add-in.
VB Code:
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
cmdBrowseSLXContacts.Delete False '<----
Set cmdBrowseSLXContacts = Nothing
Set oWord = Nothing
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 4th, 2005, 01:15 PM
#8
Re: How do I delete a command bar button from a word addin
cmdBrowseSLXContacts.Delete False
Eureka! Why's it always something so simple that roadblocks you for an entire day!!
Thanks Rob, I'll try that tomorrow when I get back to work. I'd prefer to delete the button rather than check for it's existence, just seems neater somehow. (Actually I'll probably do both)
-
Jul 4th, 2005, 01:20 PM
#9
Re: How do I delete a command bar button from a word addin
You can create a button 2 ways. Temporary and permenant. Temp will automatically delete itself when you close the
app and will be for that session only. Permenant will remain between runs and across all instances.
If youchange the parameter to True then it will delete the button for that instance only. False will be to delete it permenantly.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 5th, 2005, 09:56 AM
#10
Re: How do I delete a command bar button from a word addin
Hmm, when I came to try using the temporary parm it didn't seem to work.
I tried setting it to true on the creation of the button thinking that way I'd be creating a temporary button which would delete itself when the app closed. Result, when I reopen the app I've got 2 buttons (I'd taken my pre-check out, of course)
I tried issueing the delete with temp set to both true and false (I think from your post it should be false - cos I'm permanently deleting the button?) either way I still seemed to have an extra button when I started the app back up.
Am I missing something obvious? Unless I do the pre-check I end up with so many buttons I'm worried they're going to form their own community and attempt to overthrow the creator (ie me )
-
Jul 5th, 2005, 11:33 AM
#11
Re: How do I delete a command bar button from a word addin
The .FindControl will only work if the button is a child of that bar. You have two boolean parameters with FindControl, one
for recursive searching and another for searching visible or invisible buttons. Check them out because the FindControl is not "finding"
your button so it adds another one.
The .Delete should take care of it regardless.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 24th, 2008, 03:57 AM
#12
New Member
Re: How do I delete a command bar button from a word addin
I used the following code and it worked with me
This deletes the button created from the addin in winword using vb 6
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
'Dim omissing As Object
'omissing = System.Reflection.Missing.Value
MsgBox "My Addin was disconnected by " & _
IIf(RemoveMode = ext_dm_HostShutdown, _
"Excel shutdown.", "end user.")
Set oXL = Application
oXL.CommandBars("Standard").Controls(26).Delete
Set oXL = Nothing
End Sub
-
Dec 24th, 2008, 04:37 AM
#13
New Member
Re: How do I delete a command bar button from a word addin
Here is a better example. Make a prayer for me.
Option Explicit
Dim oWord As Word.Application
Dim WithEvents oButton As Office.CommandBarButton
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Set oWord = Application
Set oButton = oWord.CommandBars("Standard").Controls.Add(msoControlButton)
oButton.Caption = "Custom Command"
oButton.Style = msoButtonCaption
oButton.Tag = "Custom_Command_From_MyAddin"
MsgBox "Added custom Commandbar control to the File menu."
oButton.OnAction = "!<" & AddInInst.ProgId & ">"
oButton.Visible = True
End Sub
Private Sub oButton_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
MsgBox "Our CommandBar button was pressed!"
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
Set oButton = _
oWord.CommandBars.FindControl(Tag:="Custom_Command_From_MyAddin")
oButton.Delete
oWord.NormalTemplate.Save
MsgBox "Removed custom Commandbar control from the File menu."
Set oButton = Nothing
Set oWord = Nothing
End Sub
Last edited by sherifomran; Dec 24th, 2008 at 04:51 AM.
-
Dec 26th, 2008, 05:54 PM
#14
Re: How do I delete a command bar button from a word addin
You should use the OnBeginShutdown event if you are deleting buttons etc.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|