Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:32:44+00:00 2026-05-16T23:32:44+00:00

Loading an XLS file is a bit of a pain for a quick app

  • 0

Loading an XLS file is a bit of a pain for a quick app we’re throwing together (we know about how to do that but it’s not worth the time especially in C++) so we’re going to take the simple approach of have the user export a CSV copy. However to save them the trouble I wondered if we can have a macro which will automatically save a CSV version whenever they save the XLS(X) in Excel 2007?

Update:
Following Timores’ answer, I dug in a bit and came up with this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFileName As String

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook
    TempFileName = Sourcewb.FullName + ".csv"

    'Copy the sheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Save the new workbook and close it
    With Destwb
        .SaveAs Filename:=TempFileName, FileFormat:=xlCSV, ConflictResolution:=xlLocalSessionChanges
        .Close SaveChanges:=False
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

This works except I can’t make it force-save the CSV, rather than asking me if I want to overwrite, even after adding ConflictResolution:=xlLocalSessionChanges

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:32:45+00:00Added an answer on May 16, 2026 at 11:32 pm

    Original version:

    In the VB editor part of Excel, select “ThisWorkbok” in the left navigation menu. In the editor on the right, select Workbook on the left drop-down, and BeforeSave on the right one.

    Replace the macro by:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
        ActiveWorkbook.SaveCopyAs ActiveWorkbook.FullName + ".csv"
    End Sub
    

    This will make a copy with the CSV extension.

    Please note that an XLSX file cannot have a macro (you need an XLSM extension, or the older XLS one) and that users will need to have a medium or low level of security in order for the macro to run (or you have to sign the document).

    Edited version:

    I tested it again, after seeing the comments below. Strangely enough, it did not work like it did the first time. Here is a fixed version. Again, in the ‘This Workbook’ part of the macro editor:

    Dim fInSaving As Boolean
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    If fInSaving Then
        Exit Sub
    End If
    
    fInSaving = True
    
    Dim workbookName As String
    Dim parentPath As String
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    If SaveAsUI Then
    
        Dim result
        result = Application.GetSaveAsFilename
    
        If VarType(result) = vbBoolean Then
            If CBool(result) = False Then
                Exit Sub ' user cancelled the dialog box
            End If
        End If
    
        workbookName = fs.GetFileName(result)
        parentPath = fs.GetParentFolderName(result)
    Else
    
        workbookName = ActiveWorkbook.name
        parentPath = ActiveWorkbook.path
    End If
    
    
    Dim index As Integer
    index = InStr(workbookName, ".")
    
    Dim name As String
    name = Left(workbookName, index - 1)
    
    ' extension can be empty is user enters simply a name in the 'File / Save as' dialog
    ' so it is not computed (but hard-coded below)
    
    ' do not ask for confirmation to overwrite an existing file
    Application.DisplayAlerts = False
    
    ' save a copy
    ActiveWorkbook.SaveAs fs.BuildPath(parentPath, name & ".csv"), XlFileFormat.xlCSV
    
    ' Save the normal workbook in the original name
    ActiveWorkbook.SaveAs fs.BuildPath(parentPath, name & ".xlsm"), XlFileFormat.xlOpenXMLWorkbookMacroEnabled
    Cancel = True
    
    Application.DisplayAlerts = True
    fInSaving = False
    End Sub
    
    Private Sub Workbook_Open()
    
        fInSaving = False
    End Sub
    

    What is surprising is that calling ActiveWorkbook.SaveAs triggers the macro again => the global boolean to prevent infinite recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm loading an Excel file that has cells with time data, e.g. 08:00:00. But
When loading one view in to another with jquery i do that. In file
When loading my app all.js is throwing the following in IE8 at line 6:
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
while loading getfptex(got it from CTAN) batch file ,when i'm extracting this batch file
I am trying to read an Excel spreadsheet file with Ruby, but it is
I am running .net 4.0 asp.net app on IIS 7. I want to know
For loading time considerations I am using a runtime css file in my Flex
When loading external DLLs (not under our control) via LoadLibrary, we're hitting a problem
So I'm loading images from a web service, but the size of the images

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.