PC Review


Reply
Thread Tools Rate Thread

Code Crashing

 
 
John
Guest
Posts: n/a
 
      8th Feb 2008
When I run the following code it sometimes crashes on the first line
after the for loop start. The code adds/removes some menu items from
the 3 cell right click menus(There are 3 right click cell menus, one
for when a cell or area of cells are selected, one for entier row(s)
selected, and one for entire column(s) selected). I can get it to run
the line of code by debugging, then making the particular menu show
once, and then resuming the code. I can't get the error to repeat
consistently. Any ideas are appreciated. Thanks.


Code:
Sub Setup_Right_Click_Items()
Dim InsertIndex As Integer
Dim NewItem As CommandBarButton
Dim myIndex As CommandBarControl
Dim MenuArray

ReDim MenuArray(1 To 2, 1 To 3)
MenuArray(1, 1) = "Cell"
MenuArray(2, 1) = "Insert..."
MenuArray(1, 2) = "Row"
MenuArray(2, 2) = "Insert"
MenuArray(1, 3) = "Column"
MenuArray(2, 3) = "Insert"

For i = 1 To 3
Set myIndex = CommandBars(MenuArray(1,
i)).Controls(MenuArray(2, i))    '<--crashes here

On Error Resume Next
CommandBars(MenuArray(1, i)).Controls("Toggle
Merge").Delete
CommandBars(MenuArray(1, i)).Controls("Toggle
Wrap").Delete
CommandBars(MenuArray(1, i)).Controls("Paste As
Values").Delete

' default items i remove
CommandBars(MenuArray(1, i)).Controls("Pick From Drop-down
List...").Delete
CommandBars(MenuArray(1, i)).Controls("Add Watch").Delete
CommandBars(MenuArray(1, i)).Controls("Create
List...").Delete
CommandBars(MenuArray(1,
i)).Controls("Hyperlink...").Delete
CommandBars(MenuArray(1, i)).Controls("Look Up...").Delete
On Error GoTo 0

' move format cells to top
On Error Resume Next
CommandBars(MenuArray(1, i)).Controls("Format
Cells...").Delete
On Error GoTo 0
Set NewItem = Application.CommandBars(MenuArray(1,
i)).Controls.Add(ID:=855, before:=1)
With NewItem
.Caption = "Format Cells..."
End With
Set NewItem = Application.CommandBars(MenuArray(1,
i)).Controls("Cut")
With NewItem
.BeginGroup = True
End With

' Set up my addins
InsertIndex = myIndex.Index

Set NewItem = Application.CommandBars(MenuArray(1,
i)).Controls.Add(ID:=370, before:=InsertIndex)
With NewItem
.Caption = "Paste as Values"
.FaceId = 0
End With

InsertIndex = myIndex.Index

Set NewItem = Application.CommandBars(MenuArray(1,
i)).Controls.Add(before:=InsertIndex)
With NewItem
.Caption = "Toggle Wrap"
.OnAction = "Toggle_Wrap"
.BeginGroup = True
End With

InsertIndex = myIndex.Index

Set NewItem = Application.CommandBars(MenuArray(1,
i)).Controls.Add(before:=InsertIndex)
With NewItem
.Caption = "Toggle Merge"
.OnAction = "Toggle_Merge"
End With
Next i
End Sub
 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      8th Feb 2008
Well let me nitpick it...
Use Option Explicit as the first line in the module.
Make sure the code is in a standard module.
Use Application.CommandBars instead of CommandBars
Declare the i variable... Dim i as Long
Declare MenuArray as an array... Dim MenuArray() as Long

Try your code without using the myIndex variable.
All of the new menu items will then be added to the bottom of the menu.
Don't run your code in Page Break Preview mode.
There is a separate right-click "Cell" menu that functions there.

I would also have two separate subs, one to delete the menus and
one to add the menus.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"John"
<(E-Mail Removed)>
wrote in message
When I run the following code it sometimes crashes on the first line
after the for loop start. The code adds/removes some menu items from
the 3 cell right click menus(There are 3 right click cell menus, one
for when a cell or area of cells are selected, one for entier row(s)
selected, and one for entire column(s) selected). I can get it to run
the line of code by debugging, then making the particular menu show
once, and then resuming the code. I can't get the error to repeat
consistently. Any ideas are appreciated. Thanks.

-snip-
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      8th Feb 2008
Tried all of that. No luck =/ I have been able to make it repeatedly
crash trying to do add the items to the row right click menu by
opening excel and then loading the addin (I have the code placed in a
standard module in the add on xla file). I know I could let it just
add it to the end of the right click menus, but that's not where I
want it. That and I know it worked fine when I first wrote the code,
so not sure why its crashing now. I'm running MS Office 2003, SP3.
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      11th Feb 2008
On Feb 8, 4:16*pm, John <john.ful...@pccdouglas.com> wrote:
> Tried all of that. *No luck =/ *I have been able to make it repeatedly
> crash trying to do add the items to the row right click menu by
> opening excel and then loading the addin (I have the code placed in a
> standard module in the add on xla file). *I know I could let it just
> add it to the end of the right click menus, but that's not where I
> want it. *That and I know it worked fine when I first wrote the code,
> so not sure why its crashing now. *I'm running MS Office 2003, SP3.


Replacing the offending line with the following code seems to have
"fixed" the problem.

On Error Resume Next
Set myIndex =
Application.CommandBars(MenuArray(i)).Controls("Insert...")
Set myIndex =
Application.CommandBars(MenuArray(i)).Controls("Insert")
On Error GoTo 0
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Crashing which is prevented by unprotecting VB code Sssuperdave Microsoft Excel Crashes 1 16th Jan 2009 07:28 PM
code crashing my programme??????? Tdp Microsoft Excel Misc 4 30th Oct 2008 10:34 AM
IE7 kept crashing after installing and running Code Cleaner? SnowFire Windows XP Internet Explorer 4 17th Sep 2007 09:28 AM
Re: IE7 kept crashing after installing and running Code Cleaner? PA Bear Windows Vista General Discussion 1 17th Sep 2007 09:28 AM
Re: IE7 kept crashing after installing and running Code Cleaner? PA Bear Windows Vista Performance 1 17th Sep 2007 09:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:55 PM.