PDA

View Full Version : EnableMenuItem function help



dansam
02-11-2007, 01:28 AM
hi,
i found an example to disable the cut copy past commands ( http://www.vbaexpress.com/kb/getarticle.php?kb_id=373 (http://www.vbaexpress.com/kb/getarticle.php?kb_id=373) ). so it uses the
Call EnableMenuItem(21, Allow) ' cut
Call EnableMenuItem(19, Allow) ' copy
Call EnableMenuItem(22, Allow) ' paste
Call EnableMenuItem(755, Allow) ' pastespecial

and other code.........


so, if i want to disable the save and save as command so which id should i use instead of the Highlighted numbers ?


or any of you have the sample workbook that has the save and save as command are disabled (from file>save , file > save as , the sav buttons on toolbar and the ctrl + s method ) and also notice one thing that i just want to disable these normal ways ONLY . A user can save that workbook using a macro(is allowed ) so any way ????????? :think:

xld
02-11-2007, 03:44 AM
&New...: 18
&Open...: 23
&Close: 106
&Save: 3
Save &As...: 748
Save as Web Pa&ge...: 3823
Save &Workspace...: 846
Searc&h...: 5905
Ch&eck Out: 6127
Ch&eck In: 6128
We&b Page Preview: 3655
Page Set&up...: 247
Prin&t Area: 30255
Print Pre&view: 109
&Print...: 4
Sen&d To: 30095
Propert&ies: 750
Acquire Text...: 1
Acquire Text Settings...: 1
E&xit: 752

I got this with



Dim oCtl As CommandBarControl

For Each oCtl In Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls
Debug.Print oCtl.Caption & ": " & vbTab & vbTab & oCtl.ID
Next oCtl

dansam
02-11-2007, 04:01 AM
ok
thankyou for help

johnske
02-11-2007, 04:16 AM
In the Thisworkbook code module

Option Explicit

Private Sub Workbook_Open()
With Application
.OnKey "^{s}", "DontSaveMsg"
.VBE.CommandBars("File").Controls("Save " & ActiveWorkbook.Name).Enabled = False
With .CommandBars("File")
.Controls("Save").Enabled = False
.Controls("Save As...").Enabled = False
End With
End With
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Application
.VBE.CommandBars("File").Controls("Save " & ActiveWorkbook.Name).Enabled = True
With .CommandBars("File")
.Controls("Save").Enabled = True
.Controls("Save As...").Enabled = True
End With
End With
End Sub

In a standard code module

Option Explicit

Private Sub DontSaveMsg()
MsgBox "Save is disabled"
End Sub

dansam
02-11-2007, 08:06 AM
hi xld,
how to use this ?
Dim oCtl As CommandBarControl

For Each oCtl In Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls
Debug.Print oCtl.Caption & ": " & vbTab & vbTab & oCtl.ID
Next oCtl

xld
02-11-2007, 08:51 AM
That is just a simple bit of code that produced the list I gave in my previous response.

Put it in a macro and run it. You can change File to Edit, Tools, etc. to get the other ids.

dansam
02-11-2007, 09:14 AM
hi xld,
sorry but i can't understand. I paste that code in a macro and run it . But it gives nothing output ! I'm surely doing some mistake . Please point me to that . :think:

xld
02-11-2007, 09:30 AM
It outputs to the immediate window in the VBIDE, that is what Debug.Print does.

dansam
02-11-2007, 09:31 AM
xld,
oh i got it ! thankyou ...............:yes :rotlaugh:

dansam
02-11-2007, 09:34 AM
so can i change its caption ? (like file>new to file>new1)??

xld
02-11-2007, 09:42 AM
Try it and see.

dansam
02-12-2007, 01:51 AM
hi xld,
By using that macro , i can disable the save save as buttons in office 2003 . but when i tried it in office 2007 that did'nt work ! (save button was enabled!)
so , wht's about office 2007 ? (in office 2007 there are no menus like file , edit... but there is a office button at the titlebar so , if posssible , i want to disable the whole office button. I tried using your code to get those ids but .. no success ! :( )
any idea ?

Regards ,
dansam

xld
02-12-2007, 02:42 AM
Office 2007 is obviously different.

Off the top I have no idea if you can disable the Office button, and if so how. If I get a minute, I will see what I can see, but knowing that the ribbon is not exposed in the same way that commandbars are, I wouldn't be surprised if you can't do it.