Consulting

Results 1 to 11 of 11

Thread: Automatically saving workbook containing macros as a macro-free workbook

  1. #1
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Automatically saving workbook containing macros as a macro-free workbook

    Automatically saving workbook containing macros as a macro-free workbook:

    I have a workbook that contains buttons and macros. I run a macro that populates this workbook with data from other workbooks and then save it without buttons and as a macro-free workbook.
    However, when it gets to the “saving as” bit I get the following message box with options “Yes”, “No” and “Help”. I want to avoid this prompt and the option to save it in another format by always, automatically choosing the “Yes” option. Can this be done?

    I get the following prompt:

    The following features cannot be saved in macro-free workbooks:

    · VB project
    To save a file with these features, click No, and then choose a macro-enabled file type in the File Type list.
    To continue saving as a macro-free workbook, click Yes.

       'FileFormat 51 = xlOpenXMLWorkbook( without macros in 2007-2013):
                ActiveWorkbook.SaveAs FileName:=NewBookName, _
                  FileFormat:=51, _
                  PassWord:="", _
                  WriteResPassword:="", _
                  ReadOnlyRecommended:=False, _
                  CreateBackup:=False
    Best regards,
    vanhunk

  2. #2
    sub test()
    application.displayalerts = false
    ' your code here
    application.displayalerts = true
    end sub

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I'd prefer to use a copy of the workbook:

    Sub M_snb()
        Application.DisplayAlerts = False
        
        ThisWorkbook.Sheets.Copy
        With Workbooks(Workbooks.Count)
            .SaveAs ThisWorkbook.Path & "\copy_001.xlsx", 51
            .Close
        End With
        
        Application.DisplayAlerts = True
    End Sub

  4. #4
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    @holycow & snb,

    Thank you for the replies. The very first thing I tried was to use "Application.DisplayAlerts = False" but for some or other reason the macro did not complete. Don't know why, but it is now working fine - well almost.

    Is it possible to be selective, i.e. can the code be changed to only show the following alert?:

    "A file named '*********************************************' already exists in this location. Do you want to replace it?
    "Yes" "No" "Cancel"

    Regards,
    vanhunk

    snb,

    Why do you prefer the copy route?

    Thanks,
    vanhunk

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If Not Dir(ThisWorkbbok.Path) = "" Then
    Result = MsgBox "File exists" + buttons as needed
    If Result = ? then
    Whatever
    End If
    End If
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You can check the existence of a file using Dir(file.fullname).
    But you'd better use an algoritm to produce a filename that doesn't exist, so the user won't be bothered with tasks that can better be automated.

    Otherwise you could 'loose' the file containing the macro.
    Last edited by snb; 09-08-2014 at 02:30 AM.

  7. #7
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Thank you SamT & snb I appreciate your inputs.

    Regards,
    vanhunk

  8. #8
    VBAX Newbie
    Joined
    Jun 2015
    Posts
    2
    Location
    Quote Originally Posted by snb View Post
    I'd prefer to use a copy of the workbook:

    Sub M_snb()
        Application.DisplayAlerts = False
        
        ThisWorkbook.Sheets.Copy
        With Workbooks(Workbooks.Count)
            .SaveAs ThisWorkbook.Path & "\copy_001.xlsx", 51
            .Close
        End With
        
        Application.DisplayAlerts = True
    End Sub
    What if i want to save that file in particular location and with custom name like with current date prefix added with file name?

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Quote Originally Posted by ram.anim View Post
    What if i want to save that file in particular location and with custom name like with current date prefix added with file name?
    Something like:
    Sub SamT_snb() 
        Application.DisplayAlerts = False 
        Dim Pre As String
    Pre = Format(Now, "yyyy-mm-dd") & "_"
        ThisWorkbook.Sheets.Copy 
        With Workbooks(Workbooks.Count) 
            .SaveAs ThisWorkbook.Path & "\" & Pre & ThisWorkbook.Name & ".xlsx", 51 
            .Close 
        End With 
         
        Application.DisplayAlerts = True 
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Newbie
    Joined
    Jun 2015
    Posts
    2
    Location
    Quote Originally Posted by SamT View Post
    Something like:
    Sub SamT_snb() 
        Application.DisplayAlerts = False 
        Dim Pre As String
    Pre = Format(Now, "yyyy-mm-dd") & "_"
        ThisWorkbook.Sheets.Copy 
        With Workbooks(Workbooks.Count) 
            .SaveAs ThisWorkbook.Path & "\" & Pre & ThisWorkbook.Name & ".xlsx", 51 
            .Close 
        End With 
         
        Application.DisplayAlerts = True 
    End Sub
    Thanks SamT. and if i want to have this workboook as readme only files then?

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Please do not quote.

    Just check in the VBEditor, Help ( F1 ), lemma 'SaveAs'

Tags for this Thread

Posting Permissions

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