Results 1 to 5 of 5

Thread: Workbook_BeforeSave - SaveAsUI value wrong

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    3

    Workbook_BeforeSave - SaveAsUI value wrong

    A co-worker's computer recently had some software removed and afterward they noticed that the Workbook_BeforeSave routine would not work properly.

    I traced the problem to the SaveAsUI = True even though the user clicked File Save, not File Save As.

    Anyone have any ideas how the SaveAsUI variable could get messed up?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Workbook_BeforeSave - SaveAsUI value wrong

    Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Workbook_BeforeSave - SaveAsUI value wrong

    Its probably not being used properly. Can you post up the code?

    SaveAsUI should be used with the GetSaveAsFilename function for when you want to override the filename and path to save the workbook as.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    3

    Re: Workbook_BeforeSave - SaveAsUI value wrong

    Here is the code:

    VB Code:
    1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    2. Dim sPath As String, i As Integer, findslash As String, part_name As String
    3. Dim msg, Style, Title, Help, Ctxt, Response, MyString, datemonth As Variant, dateday As Integer, dateforfile As String
    4. Dim desired_name As Variant, OKtoHide As Integer, readyflag As String, PriorityFlag As String
    5. Dim c As String, Z As String, PFflag As String
    6. Dim fexten As Variant
    7. Dim actual_name As String, rstring As String, k As Integer
    8. If ginloop2 Then Exit Sub
    9. vexcel = ExcelVersion
    10. If vexcel >= 12 Then
    11.    actual_name = ActiveWorkbook.Name
    12.    rstring = Right(actual_name, 5)
    13.    k = InStr(rstring, ".")
    14.    If k = 1 Then   ' have a 4 character file extension
    15.        msg = "PLEASE NOTE:  This file must be saved in Excel 2003 format with the .xls extension." & Chr(13) & Chr(13)
    16.        msg = msg & "This file is NOT ready to send to Ford in its current format."
    17.        Style = 16    ' Define buttons.
    18.        Title = "File Format Error"    ' Define title.
    19.        Response = MsgBox(msg, Style, Title)
    20.    End If
    21. End If
    22. gbInLoop = True
    23. ginsave = True
    24. Sheets("Sheet1").Activate
    25. ' construct date tag for file
    26. datemonth = MonthName(Month(Now), True)
    27. dateday = Day(Now)
    28. dateforfile = dateday & datemonth
    29.    
    30. If Sheet1.chkPrioritySiteToggle = False Then
    31.     PriorityFlag = "NP_"
    32. Else
    33.     PriorityFlag = "P_"
    34. End If
    35.     ' let user know if pre-assessment is turned on
    36. If Sheet1.Range("BC1").Value = True Then
    37.     PFflag = "Pre_"
    38. Else
    39.     PFflag = "Fin_"
    40. End If
    41. Application.ScreenUpdating = False
    42.     ' if Save button is clicked, then compare desired_name to actual_name
    43.     ' if they are different, save file as desired_name under current path and
    44.     ' give message to user with path and naming convention rules
    45.     ' else, then do nothing (make sure Save occurs correctly)
    46.  
    47.     ' if SaveAs button is clicked, then compare desired_name to actual_name
    48.     ' if they are different, save file as desired_name under current path and
    49.     ' give message to user with path and naming convention rules
    50.     ' else, let user remain in SaveAs dialog box to same to a different name or location or both
    51. OKtoHide = CheckNotEmpty("C5") + CheckNotEmpty("C7") + CheckNotEmpty("C3") + CheckNotEmpty("L5") + CheckNotEmpty("S6")
    52. If Sheet1.cmbMilestone.Value <> "" And Sheet1.cmbPMT.Value <> "" And Sheet1.cmbPartition.Value <> "" And OKtoHide = 0 Then  ' Do not hide rows - have enough Program Info to construct filename
    53.     If Len(Sheet1.Range("S6").Value) > 20 Then
    54.         part_name = UCase(Mid(Sheet1.Range("S6").Value, 1, 20))
    55.     Else
    56.         part_name = UCase(Mid(Sheet1.Range("S6").Value, 1, Len(Sheet1.Range("S6").Value)))
    57.     End If
    58.     If CheckForReady = True Or (CheckForReady = False And gfextension = True And gReady = True) Then
    59.         readyflag = "R_"
    60.     Else
    61.         readyflag = "NR_"
    62.     End If
    63.     ' assign desired_name without .xls extension
    64.     desired_name = PFflag & readyflag & UCase(Sheet1.Range("C5").Value) & "_" & Sheet1.cmbMilestone.Value & "_" & UCase(Sheet1.Range("L5").Value) & "_" & PriorityFlag & Sheet1.Range("C7").Value & "_" & part_name & "_" & dateforfile
    65.     c = "-"
    66.     For k = 1 To 9
    67.         Select Case k
    68.             Case 1: Z = "\"
    69.             Case 2: Z = "/"
    70.             Case 3: Z = ":"
    71.             Case 4: Z = "*"
    72.             Case 5: Z = "?"
    73.             Case 6: Z = """"
    74.             Case 7: Z = "<"
    75.             Case 8: Z = ">"
    76.             Case 9: Z = "|"
    77.         End Select
    78.         desired_name = Replace(desired_name, Z, c)
    79.     Next k
    80.     ' replace blanks with null
    81.     desired_name = Replace(desired_name, " ", "")
    82.    
    83.     desired_name = desired_name & ".xls"
    84.     actual_name = ActiveWorkbook.Name
    85.     If actual_name <> desired_name And (SaveAsUI = False Or vexcel >= 12) Then
    86.         ginloop2 = True
    87.         gmileflag = True
    88.        
    89.         msg = "The current file will be saved using the Ford Schedule A naming convention:" & Chr(13) & Chr(13)
    90.         msg = msg & "'" & desired_name & "'" & Chr(13) & Chr(13)
    91.         msg = msg & "The file will be saved to the path" & Chr(13)
    92.         msg = msg & ActiveWorkbook.Path & Chr(13)
    93.         If ginclose1 = False Then
    94.             If SaveAsUI = False Then
    95.                 msg = msg & Chr(13) & "If you wish to save it to a different location/name, click File, Save As" & Chr(13)
    96.             End If
    97.         End If
    98.         If vexcel >= 12 Then
    99.             msg = msg & Chr(13) & "PLEASE NOTE:  The file must be saved in Excel 2003 format." & Chr(13)
    100.         End If
    101.         Style = vbOKOnly    ' Define buttons.
    102.         Title = "File Naming Convention Check"    ' Define title.
    103.         Response = MsgBox(msg, Style, Title)
    104.         On Error GoTo 100
    105.         ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & desired_name, _
    106.         FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    107.         ReadOnlyRecommended:=False, CreateBackup:=False
    108.    
    109.     End If
    110.    
    111.     If SaveAsUI = True Then
    112.         gmileflag = True
    113.         gsaveas = True
    114.         gactive = False
    115.     ElseIf ginclose2 = False Then
    116.         Cancel = True
    117.         ginloop2 = True
    118.         ActiveWorkbook.Save
    119.         gactive = False
    120.     End If
    121.    
    122. ElseIf SaveAsUI = True Then
    123.     gmileflag = True
    124.     gsaveas = True
    125.     gactive = False
    126. Else
    127.     ginloop2 = True
    128.     ActiveWorkbook.Save
    129.     gactive = False
    130.     ginloop2 = False
    131.     ginsave = False
    132. End If
    133. 100
    134. gbInLoop = False
    135. ginloop2 = False
    136. ginsave = False
    137. gactive = False
    138.  
    139.  End Sub
    Last edited by si_the_geek; Jul 21st, 2009 at 07:35 AM. Reason: added vbcode tags

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    3

    Resolved Re: Workbook_BeforeSave - SaveAsUI value wrong

    We found a solution to this problem. Our help desk had to recreate a new user profile for the user and it corrected the problem.

Posting Permissions

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



Click Here to Expand Forum to Full Width