Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    System Control Close (Word 2002)

    Some time ago I got information, including the code, for disabling the System Control "X" in Word. I recall the the code worked well. Recently I needed to use this again, and I'm finding that there is a problem. When I run the code as an AutoExec macro, it just does not execute. (I do have Windows in Taskbar set to False.) If I put a line of code at the top of the Sub: MsgBox "About to Run" the code does execute. In all cases, the code executes as expected when run "manually" (Alt-F8 + macro or F5 from VBA Editor).

    Any idea why this is occurring? I thought perhaps that recent Windows updates might explain why this functioned correctly six months ago but not now.

    Thanks,
    Richard Barrett

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

    Re: System Control Close (Word 2002)

    I assume that your code deals with application level events, so apart from an AutoExec macro, there should be a class module and a global variable to instantiate the class module. Can you provide more information about these, and about the contents of the AutoExec macro?

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: System Control Close (Word 2002)

    Hello Hans,

    Thanks for your reply.

    I'm not sure that the application events/class module which you mention are necessary since I am not using multiple document interface (Windows in Taskbar = True). The only event I want to trap is the launch of the application... which is not an event exposed by a class module which declares Public WithEvents App as Word.Application. (If I were using MDI, I'd want to trop Doc Open, New Doc, etc.)

    Here follows the code (General and Sub) which I'm using. You'll see the MsgBox command commented out. With this enabled, there is not problem. Without this line of code, the AutoExec does not seem to run.

    Option Explicit
    Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, ByVal bRevert As Long) As Long

    Private Declare Function GetMenuItemCount Lib "user32" _
    (ByVal hMenu As Long) As Long

    Private Declare Function DrawMenuBar Lib "user32" _
    (ByVal hwnd As Long) As Long

    Private Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

    Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Const MF_BYPOSITION = &H400&
    Const MF_REMOVE = &H1000&



    Sub AutoExec() ' Remove System Control Close "X"

    'MsgBox "Ready to run."
    Dim hSysMenu As Long, nCnt As Long
    Dim hWndWD As Long
    ' Get handle to our form's system menu
    ' (Restore, Maximize, Move, close etc.)
    hWndWD = FindWindow32("OpusApp", vbNullString)
    hSysMenu = GetSystemMenu(hWndWD, False)
    If hSysMenu Then
    ' Get System menu's menu count
    nCnt = GetMenuItemCount(hSysMenu)
    If nCnt Then
    ' Menu count is based on 0 (0, 1, 2, 3...)
    RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
    ' RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the seperator
    DrawMenuBar hWndWD
    End If
    End If

    End Sub

    Thanks,
    Richard

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

    Re: System Control Close (Word 2002)

    So you're using API code. I copied your code and see the same behavior. I even had the code execute 10 seconds after starting Word, using OnTime, and I used Debug.Print to trace execution. The code does get executed, all of the API functions return success, yet the Close button is not disabled (nor is any other system menu item, if you modify the item number.) If I run the macro interactively, it works. I can't explain this, and I don't know enough about Windows API to really help. I hope that somebody else will be able to shed light on this.

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: System Control Close (Word 2002)

    When I first asked about this about six or eight months ago, I got several replies with various versions of the code... all API. I understand that this is the ONLY way to disable any of the system controls.

    I tried various other means of delay execution, but none worked. The only thing I can think of is that Word hasn't completely "settled in" at the time the code runs, and therefore the X doesn't get disabled. I figure the message box waits for Word to finish loading completely before it displays, and thus Word is ready to have the system control disabled. Just a hunch.

    Richard

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

    Re: System Control Close (Word 2002)

    Yes, API is the only way to disable the Close button; I hadn't read your original question carefully enough.

    I tested your code with Debug.Print instead of MsgBox, and used a 10 second delay. Word has completely "settled" by then. Each statement of the code is executed successfully, yet the Close button is unaffected if the code is run automatically. <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

Posting Permissions

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