+ Reply to Thread
Results 1 to 2 of 2

Deactivate X (Close button) when using FileDialog

Hybrid View

  1. #1
    Registered User
    Join Date
    09-22-2006
    Posts
    11

    Deactivate X (Close button) when using FileDialog

    When the user is selecting a file to open, I want to deactivate the X (Close
    Button) so the user can not exit out of the macro.

    Do you know if this can be done? Is there a property that needs to be set.

    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Filters.Add "Text files", "*.den"
    If .Show = True Then
    Txt = .SelectedItems(1)
    'Import data
    For x = LBound(ColumnsDesired) To UBound(ColumnsDesired)
    ColumnArray(x, 1) = ColumnsDesired(x)
    ColumnArray(x, 2) = DataTypeArray(x)
    Next x

    Workbooks.OpenText Filename:=Txt, _
    DataType:=xlFixedWidth, FieldInfo:=ColumnArray
    ActiveSheet.UsedRange.Copy Destination:=Isheet.Range("A1")
    ActiveWorkbook.Close SaveChanges:=False
    End If
    End With

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Fredriksson,

    Add a Standard Module to your VBA project and then copy and paste the code below into it. There's a lot of code, but the macro is very easy to use. Place the macro call before you call the dialog.

    Example:
    Call DisableCloseBox(250)
    Application.Dialogs(xlDialogOpen).Show


    The 250 is a time delay of 1/4 second (250 milliseconds). This has worked well for me. If you need to increase the time, you can.

    '//////////////////////////////////'
    '/                                /'
    '/ Disable Dialog Close Box Macro /'
    '/ Written Jan. 26, 2007          /'
    '/ Author: Leith Ross             /'
    '/                                /'
    '//////////////////////////////////'
    
    'Returns the Window Handle of the Window
    'that is accepting User input.
     Public Declare Function GetForegroundWindow _
      Lib "user32.dll" _
       () As Long
    
    'Retuns the Handle to the System Menu
     Private Declare Function GetSystemMenu _
      Lib "user32.dll" _
       (ByVal hwnd As Long, _
        ByVal bRevert As Long) As Long
                   
    'Returns the number Menu Items in the System Menu
     Private Declare Function GetMenuItemCount _
      Lib "user32.dll" _
       (ByVal hMenu As Long) As Long
                   
    'Allows Items to be disabled or removed from the System Menu
     Private Declare Function RemoveMenu _
      Lib "user32.dll" _
       (ByVal hMenu As Long, _
        ByVal nPosition As Long, _
        ByVal wFlags As Long) As Long
        
    'Redraw the Icons on the Window's Title Bar
     Private Declare Function DrawMenuBar _
      Lib "user32.dll" _
       (ByVal hwnd As Long) As Long
     
     Private Declare Function SetTimer _
      Lib "user32.dll" _
        (ByVal hwnd As Long, _
         ByVal nIDevent As Long, _
         ByVal uElapse As Long, _
         ByVal lpTimerfunc As Long) As Long
    
     Private Declare Function KillTimer _
      Lib "user32.dll" _
        (ByVal hwnd As Long, _
         ByVal nIDevent As Long) As Long
         
     Private Declare Function GetTickCount _
      Lib "kernel32.dll" _
        () As Long
    
    'Menu Constants
     Const MF_BYPOSITION As Long = &H400
     Const MF_DISABLED As Long = &H2
     Const MF_GRAYED As Long = &H1
     Const MF_ENABLED As Long = &H0
    
    '==========Public Timer Variable Declarations ==============================
    
    Public TimerStartTime As Long
    
    Public TimerID As Long            'Turn On and Off with this ID
    
    Public TimerActive As Boolean     'Is the timer active
    
    '============================================================
    
    
    Public Sub TurnTimerOn(ByVal MilliSec As Long)
      
        If TimerActive Then Call TurnTimerOff
    
        On Error Resume Next
          TimerID = SetTimer(0, 0, MilliSec, AddressOf TimerProc)
    
        TimerActive = True
    
    End Sub
    
    Public Sub TurnTimerOff()
    
        KillTimer 0, TimerID
    
    End Sub
    
    Private Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal nIDevent As Long, ByVal dwTime As Long)
                               
      'Disable the Close Box
       hwnd = GetForegroundWindow()
       hMenu = GetSystemMenu(hwnd, Enabled)
                    
         MIC = GetMenuItemCount(hMenu)
         Call RemoveMenu(hMenu, MIC - 1, MF_DISABLED Or MF_BYPOSITION)
    
         Call DrawMenuBar(hwnd)
        
         TurnTimerOff
    
    End Sub
    
    Public Sub DisableCloseBox(ByVal TimeDelay As Long)
     'Time Delay in milliseconds. 1 millisecond = 1/1000 of a second
     
        TurnTimerOn TimeDelay
    
    End Sub
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1