Macro to disable cutfunction

J

Jonsson

Hi,

I have tried to construct a code to open the workbook withou
commandbars and without the cut-function.

But when I open the workbook, I get "compile-error", and when openin
the module it says I need to define the type
"Dim oCtls As CommandBarsControls".
I use this in a workbook that contains a lot of macros and sheets, bu
when I test in a new workbook it works fine.

Can anyone help to get it right?

Here´s the code:

Sub Auto_Open()



With ActiveWindow
Application.CommandBars(1).Enabled = False 'True to restore

.DisplayHeadings = False
.DisplayWorkbookTabs = False
End With
Application.CommandBars("Standard").Visible = False
Application.CommandBars("Formatting").Visible = False
Application.CommandBars("Forms").Visible = False
Application.CommandBars("Drawing").Visible = False
Application.CommandBars("Visual Basic").Visible = False

Dim oCtls As CommandBarControls
Dim oCtl As CommandBarControl
Set oCtls = CommandBars.FindControls(ID:=21) ''Cut
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = False
Next
End If
Set oCtls = CommandBars.FindControls(ID:=522) ''Options
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = False
Next
End If
With Application
.OnKey "^x", ""
.OnKey "+{Del}", ""
.CellDragAndDrop = False

End With
End Sub


Function Auto_Close()

With ActiveWindow
Application.CommandBars(1).Enabled = True 'True to restore

.DisplayHeadings = False

End With
Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
Application.CommandBars("Drawing").Visible = True
Dim oCtls As CommandBarControls, oCtl As CommandBarControl
Set oCtls = CommandBars.FindControls(ID:=21)
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = True
Next
End If
Set oCtls = CommandBars.FindControls(ID:=522)
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = True
Next
End If
With Application
.OnKey "^x"
.OnKey "+{Del}"
.CellDragAndDrop = True
End With

Application.Quit
End Functio
 
D

DNF Karran

Sounds like the reference to the toolbars object is missing in your cod
file as it works in a new workbook.

Open the VBE and go Tools/References.

look down the list for "Microsoft office x.x object library" and ensur
this is checked.

Dunca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top