Excel VBA Problem : UserForm1.show issue (shows up twice)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bennyad
    New Member
    • Dec 2007
    • 1

    #1

    Excel VBA Problem : UserForm1.show issue (shows up twice)

    hi all,

    i've created an Excel VBA script that shows UserForm1 when clicking on close (the "X" at the top right) without saving. the form simply prompts the user whether to save the file or not (with few more options).
    the problem that occurs is: after clicking the submit button, the appears again and only after the second time i pressed the submit button it disappears.
    in short: the form appears twice instead of just once (like i want).
    my code:

    Private Sub Workbook_Before Close(Cancel As Boolean)
    If ThisWorkbook.Sa ved = False Then
    If ThisWorkbook.Fi leFormat = xlTemplate Then
    UserForm1.Show
    End If
    End If
    End Sub



    ' The submit code:


    Private Sub cmdSubmit_Click ()
    If UserForm1.NoSav ePrint = True Then
    UserForm1.Hide
    ThisWorkbook.Cl ose False
    End If
    If UserForm1.NoSav ePrint = False Then
    UserForm1.Hide
    Application.Dis playAlerts = False
    Application.Scr eenUpdating = False
    ThisWorkbook.Sa ve
    ActiveWorkbook. SaveAs Filename:="\\sb s2003\OrdersTnu a\JetBackup\Ord erNo_" & Range("E8"), FileFormat:=xlN ormal, Password:="jet" , WriteResPasswor d:="", ReadOnlyRecomme nded:=False, CreateBackup:=F alse
    Application.Dis playAlerts = True
    Application.Scr eenUpdating = True
    Call ThisWorkbook.Pr int2
    ThisWorkbook.Cl ose
    End If
    End Sub



    i tried and tried and could not find out where's the error. everything is great except the fact that the form repeats twice.

    will be grateful for any ideas.
    thanks
  • daniel aristidou
    Contributor
    • Aug 2007
    • 494

    #2
    Originally posted by bennyad
    hi all,


    Code:
    [I]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ThisWorkbook.Saved = False Then
       If ThisWorkbook.FileFormat = xlTemplate Then
          [B]UserForm1.Show[/B]
       End If
    End If
    End Sub[/I]
    ' The submit code:


    [
    Code:
    I]Private Sub cmdSubmit_Click()
    If UserForm1.NoSavePrint = True Then
       [B]UserForm1.Hide[/B]
       ThisWorkbook.Close False
    End If
    If UserForm1.NoSavePrint = False Then
       [B]UserForm1.Hide[/B]
       Application.DisplayAlerts = False
       Application.ScreenUpdating = False
       ThisWorkbook.Save
       ActiveWorkbook.SaveAs Filename:="\\sbs2003\OrdersTnua\JetBackup\OrderNo_" & Range("E8"), FileFormat:=xlNormal, Password:="jet", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        Call ThisWorkbook.Print2
        ThisWorkbook.Close
    End If
    End Sub[/I]
    i
    Can you please submit comments into your code explaining what each line does. This will make it allot easier to solve your problem.
    Reading comments help anormously when dealing with this type of problem

    Daniel(~_~)

    Comment

    • daniel aristidou
      Contributor
      • Aug 2007
      • 494

      #3
      instead of this code
      Originally posted by bennyad
      Code:
      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If ThisWorkbook.Saved = False Then
         If ThisWorkbook.FileFormat = xlTemplate Then
            [B]UserForm1.Show[/B]
         End If
      End If
      End Sub

      Why not use
      Code:
      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If ThisWorkbook.Saved = False And ThisWorkbook.FileFormat = xlTemplate Then
               [B]UserForm1.Show[/B]
      
      End If
      End Sub

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Subscribing (please ignore this post).

        Comment

        Working...