Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disable 'Page Setup' menu option (WORD 97/2000)

    I have searched the forum and could not find anything on it.

    Is there a way of disabling the 'Page Setup' menu option on the File menu, i.e. grey it out? It is for a template.

    I know I can attach a macro to Page Setup so that when the user selects Page Setup, word runs my macro (which does nothing). So it should really be greyed out since no action is taken by clicking that option. (Surely it must be possible to do this by setting some options when a document is opened/created using a particular template).

    Thanks in advance.

    Robie
    Thanks.
    Robie

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disable 'Page Setup' menu option (WORD 97/2000)

    <P ID="edit" class=small>(Edited by carbonnb on 05-Dec-02 08:04. Because it won't work as expected, or at least it didn't work as I expected it to.)</P>This should do what you need:

    Dim cmb As CommandBar
    Dim cmbctl As CommandBarPopup
    Dim cmbctl2 As CommandBarControl
    <font color=448800>'Set Reference to Menu Bar</font color=448800>
    Set cmb = ActiveDocument.CommandBars("Menu Bar")
    <font color=448800>'Set Reference to File Menu on Menu Bar</font color=448800>
    Set cmbctl = cmb.Controls(1)
    <font color=448800>'Set Reference to Page Setup on File Menu on Menu Bar</font color=448800>
    Set cmbctl2 = cmbctl.Controls(9)
    <font color=448800>'Disable Page Setup</font color=448800>
    cmbctl2.Enabled = False

    Ignore this post
    This will disable the Page Setup completely, even if you switch documents.

    Hans has a better way to do it.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disable 'Page Setup' menu option (WORD 97/2000)

    Hans you are the man! <img src=/S/cool.gif border=0 alt=cool width=15 height=15> <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> Oh, how shall I put it - PERFECTO MUNDO. Thanks a bunch. Bryan thanks for your suggestion as well. <img src=/S/salute.gif border=0 alt=salute width=15 height=20>
    Thanks.
    Robie

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Selectively disable menu option (Word 97/2000)

    For anyone trying to get the Command ID of a menu items, my earlier code doesn't work because you have to get past the first command bar ("Menu Bar"). You can use this instead:
    <pre>Sub ShowControlIDsForMenu()
    'Requires Word 2000 or higher to use the Split() function
    Dim strBar As String, strArray() As String, cBarCtrl As CommandBarControl
    ' Prompt for name of command bar.
    strBar = InputBox("Enter name of menu, or a menu and sub-menu, separated with " & _
    "a colon (e.g., File:Send To)")
    ' Get out if user canceled.
    If strBar = "" Then Exit Sub
    ' Set up array
    strArray() = Split(strBar, ":")
    ' Loop through command bar controls.
    Select Case UBound(strArray)
    Case 0
    For Each cBarCtrl In CommandBars("Menu Bar").Controls(strBar).Controls
    ' Output caption and ID to the Immediate window
    Debug.Print cBarCtrl.Caption, cBarCtrl.ID
    Next
    Case 1
    For Each cBarCtrl In CommandBars("Menu Bar").Controls(strArray(0)). _
    Controls(strArray(1)).Controls
    ' Output caption and ID to the Immediate window
    Debug.Print cBarCtrl.Caption, cBarCtrl.ID
    Next
    Case Else
    MsgBox "Unable to process input... too many colons"
    Exit Sub
    End Select
    ' Release object memory.
    Set cBarCtrl = Nothing
    End Sub</pre>

    I was a bit lazy and used the Split() function, which did not exist in VBA 5 (Word 97). It should work for Word 2000 and 2002.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Selectively disable menu option (Word 97/2000)

    <P ID="edit" class=small>(Edited by HansV on 18-Dec-02 17:21. Added link to Jefferson's post on finding ID's of submenu items.)</P>Here is a method to selectively enable/disable a menu option

    Let's say that you want to disable a menu item when a document based on your template is open, and to enable it when a document based on another template is open. I will take the Page Setup item in the File menu as an example. The best way to refer to a built-in menu item or toolbar button is by its ID; it would also be possible to use its caption, but the ID of a command bar control is language-independent, while the caption isn't.

    How can I find this ID, you ask? Use this macro from <post#=200111>post 200111</post#> by Jefferson Scher. It will prompt for the name of a command bar and then list the caption and ID of each control in the Immediate window. Note: if you want to find the ID for an item in a submenu, use the code in <post#=204976>post 204976</post#>, also by Jefferson.

    Sub ShowControlIDs()
    Dim strBar As String, cBarCtrl As CommandBarControl
    ' Prompt for name of command bar.
    strBar = InputBox("Enter name of toolbar")
    ' Get out if user canceled.
    If strBar = "" Then Exit Sub
    ' Loop through command bar controls.
    For Each cBarCtrl In CommandBars(strBar).Controls
    ' Output caption and ID.
    Debug.Print cBarCtrl.Caption, cBarCtrl.ID
    Next
    ' Release object memory.
    Set cBarCtrl = Nothing
    End Sub

    In this case, enter "File" (without the quotes) when prompted. It turns out that he Page Setup menu item has ID = 247.

    Here are the steps to follow:

    1. <LI>Make a backup copy of your template - it's wise to have a copy in case things go wrong.
      <LI>Open the template in Word.
      <LI>Activate the Visual Basic Editor (Tools/Macro/Visual Basic Editor or Alt+F11).
      <LI>Make sure the Project Explorer is visible (View/Project Explorer or Ctrl+R).
      <LI>Locate your template in the Project Explorer. If necessary, expand it, then expand Microsoft Word Objects.
      <LI>Double click ThisDocument to open the associated code window.
      <LI>Enter the following code into the window (or use copy and paste).

      ' Declare an application object variable
      Private WithEvents appWrd As Word.Application

      Private Sub appWrd_DocumentChange()
      Dim ctl As CommandBarControl
      ' Get a reference to the menu item.
      Set ctl = CommandBars("File").FindControl(ID:=247)
      ' (ActiveDocument.AttachedTemplate = MacroContainer) returns a flag (True/False)
      ' True if this code is in the template attached to the active document.
      ' False otherwise.
      ' If you want the menu item to be *enabled* if the active document belongs to this template,
      ' remove Not from the next line.
      ctl.Enabled = Not (ActiveDocument.AttachedTemplate = MacroContainer)
      ' Release object memory.
      Set ctl = Nothing
      End Sub

      Private Sub Document_Close()
      ' Release object memory
      Set appWrd = Nothing
      End Sub

      Private Sub Document_New()
      If appWrd Is Nothing Then
      ' Initialize
      Set appWrd = Word.Application
      End If
      End Sub

      Private Sub Document_Open()
      If appWrd Is Nothing Then
      ' Initialize
      Set appWrd = Word.Application
      End If
      End Sub

      <LI>Switch back to Word.
      <LI>Save and close the template.
    Test the result before releasing the template to others. Create a new document based on the template, and also create a standard new document (Ctrl+N), or open an existing document based on another template than the one you've been working on.
    If everything went OK, the menu item (Page Setup in this example) is enabled/disabled correctly when you switch windows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •