+ Reply to Thread
Results 1 to 9 of 9

Restrict File Save in .xls format

Hybrid View

chasoe Restrict File Save in .xls... 02-15-2019, 04:31 AM
bakerman2 Re: Restrict File Save in... 02-15-2019, 04:44 AM
chasoe Re: Restrict File Save in... 02-15-2019, 04:52 AM
bakerman2 Re: Restrict File Save in... 02-15-2019, 05:44 AM
chasoe Re: Restrict File Save in... 02-15-2019, 06:02 AM
bakerman2 Re: Restrict File Save in... 02-17-2019, 09:15 PM
chasoe Re: Restrict File Save in... 02-17-2019, 10:17 PM
bakerman2 Re: Restrict File Save in... 02-17-2019, 11:14 PM
chasoe Re: Restrict File Save in... 02-17-2019, 11:56 PM
  1. #1
    Forum Contributor
    Join Date
    03-27-2015
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    147

    Restrict File Save in .xls format

    Dear Experts,

    I consistently receive many Excel report files with macros in them, but some files received have been mistakenly removed the macros due to saving them under .xlsx format.

    For this reason, I've put the below macro in VBA Project Explorer - Workbook TAB to restrict saving the file in .xls. However there is a flaw in that this macro in that, when we save the file it will not prompt the default folder from which the file is opened, but prompt other folders especially when other workbooks are opened from "other" folders.

    I've searched from internet and created a macro as below :

    
    Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
         
        Dim strFileName As String
        Const strRestrictedName As String = "False"
        
        Application.EnableEvents = False
     
        Cancel = True
     
        strFileName = Application.GetSaveAsFilename(fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")
        
        If UCase$(strFileName) = UCase$(strRestrictedName) Then
        
          MsgBox "Invalid File Name", vbCritical, "Stop"
          Application.EnableEvents = True
        
        ElseIf strFileName = "" Then
        
           MsgBox "Invalid File Name", vbCritical, "Stop"
           Application.EnableEvents = True
           
        Else
            
            ActiveWorkbook.SaveAs strFileName
            Application.EnableEvents = True
        
        End If
     
        Application.EnableEvents = True
     
    End Sub
    Would like someone who can help fine-tune the scripts.

    Many thanks !

    Edward

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Restrict File Save in .xls format

    You can set InitialFileName when using GetSaveAsFilename.
    strFileName = Application.GetSaveAsFilename(InitialFileName:="D:\Test", fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")
    As for SaveAs you can set default FileFormat
    ActiveWorkbook.SaveAs strFileName, 50
    Does this help you any further ?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    03-27-2015
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    147

    Re: Restrict File Save in .xls format

    But the file can be in any folder, and will "InitialFileName:="D:\Test" be inappropriate ?
    The file will be saved under different folder each month, so fixing the folder to D:\Test is not possible.

    Any further advice ?

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Restrict File Save in .xls format

    Do you open the file via code or is it opened manually.

    If it's opened manually how can Excel ever know from which directory the file was opened ?

  5. #5
    Forum Contributor
    Join Date
    03-27-2015
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    147

    Re: Restrict File Save in .xls format

    The file is opened manually.

    In fact, without the codes, a normal file is saved by defaut to existing folder from which the file is originally opened (without any screen prompts), and if you close the file with file changes the system wil prompt you whether you wish to save the file with changes you made.
    And only when you use SAVEAS, there will be a pop-up screen asking user which folder to save.

    I just wish to retain the above feature, but the codes I used will always prompt me where to save, and worst of all, not prompt me the default folder from which the file is opened, and I often saved the file to the wrong folder different from the folder it originally resides.

    See if you understand what I expect.

    Thx again.

    Regards,

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Restrict File Save in .xls format

    How about this.
    strFileName = Application.GetSaveAsFilename(InitialFileName:=Thisworkbook.path, fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")

  7. #7
    Forum Contributor
    Join Date
    03-27-2015
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    147

    Re: Restrict File Save in .xls format

    Thx for your further advice.

    I've just test other alternative basing on your suggestion to add "Thisworkbook.path", by adding Workbook Name after it.

    strFileName = Application.GetSaveAsFilename(InitialFileName:=ThisWorkbook.Path & "\" & ActiveWorkbook.Name, fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")
    This is close enough to what I expected, but I've another doubt :

    For some unknown reason, the codes sometimes seem not able to take control (as if they are absent), file save or saveas will just return to normal file save feature (ie will not restrict to .xls, etc). Seems this will happen especially when I've also opened other workbooks in same Excel and perform some other tasks.

    Any idea ?

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Restrict File Save in .xls format

    Setting Fileformat to 50 on SaveAs will always save your file as '.xls'.

  9. #9
    Forum Contributor
    Join Date
    03-27-2015
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    147

    Re: Restrict File Save in .xls format

    OK. In fact, I've tried adding Fileformat 50 to the codes, but since I cannot figure out correctly the upper portion of the codes, so I didn't notice the effect of adding "50" to the codes.

    I seem to understand now on my doubt for the codes sometimes not able to take control of SAVE/SAVEAS function :
    Perhaps when I'm debugging the codes, and when the codes run up to just beyond the 3rd line as below (which will disable the macro) and stops with code error, then subsequent SAVE / SAVEAS will not call up the codes again.
    Application.EnableEvents = False
    In any case, my problem is solved now.

    Many thanks indeed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. vba to save the file in two different format
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-16-2018, 05:48 AM
  2. Replies: 4
    Last Post: 03-20-2016, 04:27 PM
  3. how to save as txt file in this format please
    By 3ammary1 in forum Excel General
    Replies: 2
    Last Post: 04-23-2012, 04:45 AM
  4. How to: Open file, format data, save file, close file and repeat.
    By thexeber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2010, 12:56 PM
  5. Save file in different format via VBA
    By jGLZa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2010, 10:35 AM
  6. Save as file format
    By boc_est1986 in forum Word Programming / VBA / Macros
    Replies: 0
    Last Post: 08-19-2009, 08:30 AM
  7. [SOLVED] Save excel file as another format file
    By ExcelReport in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2005, 09:06 AM

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