Results 1 to 3 of 3

Thread: [Office] auto-convert .xls to .csv

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    29

    [Office] auto-convert .xls to .csv

    Hello all,

    I have an interesting request. I'm pretty new to writing macro's for Office, but I'm no stranger to VB. The issue I'm having is:

    I need to be able to convert a .xls file to a .csv file from a script. I know that office allows you to pass a macro to be executed from the command line via the /m switch. The problem is getting the macro to work.

    I'm using Office 2003, and if i record a macro, then go to file_saveas, change the filetype to .csv, it all works fine. Except for the fact that it asks the user for input, example: If there are more than one sheets in the workbook, it will pop up with a message box informing you. Also another message box pops up saying "File may contain features that are not compatible with CSV ...." do you want to keep the .csv format? and it has a Yes, No, and Help button.

    I was wondering if there is a way to force it to save in the format without asking for user input (so it is script friendly).

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Office] auto-convert .xls to .csv

    you can automate excel to do what you want, but that is different from shelling the whole thing from the command line
    as you know, you can only save 1 sheet to csv, so you can copy that sheet to temp workbook, or delete all other sheets first, when you saveAs the original workbook will be closed with no changes

    you can use application.displayalerts = false to stop most warnings, but i doubt that will be satisfactory to avoid warning about too many sheets, there is also option in saveAs
    from msdn
    SaveAs Method


    Saves changes to the sheet (Syntax 1) or workbook (Syntax 2) in a different file.

    Syntax 1

    expression.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AddToMru, TextCodePage, TextVisualLayout)

    Syntax 2
    syntax1 is to save a sheet, syntax 2 is to save workbook, the sheet syntax is what you want in this case
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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

    Re: [Office] auto-convert .xls to .csv

    Here is the reverse csv to xls. Just an example but if you change it around it can easily be made to do xls to csv.

    http://www.vbforums.com/showthread.php?t=408487
    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

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