Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing toolbars from MS Word

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

I've written a macro that will delete a toolbar button and two other toolbars.

Problem is, if one (or more) of the toolbars doesn't exist I get an error:

Code:
Run-time error '5':

Invalid procedure call or argument

Yet I've added in 'On Error Goto' statements to avoid errors!


Code:
Sub Main()
'
' AutoExec Macro
' Macro created 31/05/2006 by JefferyJ
'

Delete1:
    On Error GoTo ErrorMessage1
    CommandBars("Standard").Controls("CORP").Delete
    msg = "CORP Toolbar button deleted"
Delete2:
    On Error GoTo ErrorMessage2
        CommandBars("CORP Reports").Delete
        msg = msg & Chr(10) & "CORP Reports Toolbar deleted"
Delete3:
    On Error GoTo ErrorMessage3
    CommandBars("CORP Styles").Delete
    msg = msg & Chr(10) & "CORP Styles Toolbar deleted"
    
    GoTo TheEnd

ErrorMessage1:
    msg = "No CORP Toolbar button found!"
    GoTo Delete2
ErrorMessage2:
    msg = msg & Chr(10) & "No CORP Reports Toolbar found!"
    GoTo Delete3
ErrorMessage3:
    msg = msg & Chr(10) & "No CORP Styles Toolbar found!"

TheEnd:
    MsgBox (msg)

End Sub

So, if the toolbar exists, the macro deletes it - all good. But if it doesn't I get a compile-time error. What gives?

JJ
 
JPJeffery,
In the VBE pane goto:
Tools => Options
General tab
In the Error Trapping section are you set to Break on Unhandled Errors?

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Looks like too many error handlers, after commenting the first part the second works OK. Why not use structures like:
Code:
On Error Resume Next
CommandBars("Standard").Controls("CORP").Delete
If Err.Number = 0 Then
    msg = "CORP Toolbar button deleted"
Else
    msg = "No CORP Toolbar button found!"
    Err.Clear
End If
CommandBars("Standard").Controls("CORP").Delete
If Err.Number = 0 Then
    msg = msg & Chr(10) & "CORP Reports Toolbar deleted"
Else
    msg = msg & Chr(10) & "No CORP Reports Toolbar found!"
    Err.Clear
End If
CommandBars("CORP Styles").Delete
If Err.Number = 0 Then
    msg = msg & Chr(10) & "CORP Styles Toolbar deleted"
Else
    msg = msg & Chr(10) & "No CORP Styles Toolbar found!"
    Err.Clear
End If
MsgBox (msg)

combo
 
JPJeffery,
I got your original routine to work by replacing the [tt]Goto[/tt] with [tt]Resume[/tt] in the Error Handlers.
Code:
Sub Main()
'
' AutoExec Macro
' Macro created 31/05/2006 by JefferyJ
'

Delete1:
    On Error GoTo ErrorMessage1
    CommandBars("Standard").Controls("CORP").Delete
    msg = "CORP Toolbar button deleted"
Delete2:
    On Error GoTo ErrorMessage2
        CommandBars("CORP Reports").Delete
        msg = msg & Chr(10) & "CORP Reports Toolbar deleted"
Delete3:
    On Error GoTo ErrorMessage3
    CommandBars("CORP Styles").Delete
    msg = msg & Chr(10) & "CORP Styles Toolbar deleted"
    
    GoTo TheEnd

ErrorMessage1:
    msg = "No CORP Toolbar button found!"
    [b]Resume[/b] Delete2
ErrorMessage2:
    msg = msg & Chr(10) & "No CORP Reports Toolbar found!"
    [b]Resume[/b] Delete3
ErrorMessage3:
    msg = msg & Chr(10) & "No CORP Styles Toolbar found!"

TheEnd:
    MsgBox (msg)

End Sub

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
THAT, Sir, is very nearly the solution I'd thought of...but better!

Mine was going to be something along the lines of (i.e. I've not tested this!)

Code:
Sub Main()
'
' AutoExec Macro
' Macro created 31/05/2006 by JefferyJ
'
    On Error GoTo ErrorHandler
    msg = "TOOLBAR RESETS RESULTS:" & Chr(10)
Delete1:
    DeleteTarget = A
    CommandBars("Standard").Controls("CORP").Delete
    msg = "CORP Toolbar button deleted"
Delete2:
    DeleteTarget = B
    CommandBars("CORP Reports").Delete
    msg = msg & Chr(10) & "CORP Reports Toolbar deleted"
Delete3:
    DeleteTarget = C
    CommandBars("CORP Styles").Delete
    msg = msg & Chr(10) & "CORP Styles Toolbar deleted"
    
    GoTo TheEnd

ErrorHandler:
  Select Case DeleteTarget
  Case A
    msg = msg & Chr(10) & "No CORP Toolbar button found!"
  Case B
    msg = msg & Chr(10) & "No CORP Reports Toolbar found!"
  Case C
    msg = msg & Chr(10) & "No CORP Styles Toolbar found!"
  End Select
  ' How do you end an Error Handler?
TheEnd:
    MsgBox (msg)

End Sub

That was the idea I'd had whilst, um, 'freshening up'...

JJ
 
How do you end an Error Handler?
With a Resume instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And that works too!

I'm really getting to love VBS/VBA...

JJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top