Solved

Using Excel9.CPP to convert .xls to .txt

Posted on 2004-08-02
5
648 Views
Last Modified: 2013-11-20
i'm trying to use the following function to convert an excel file to a tab delimited file.

The problem is I don't understand the VARIANT params and how to populate them.
Here is the function:

void _Workbook::SaveAs(const VARIANT& Filename, const VARIANT& FileFormat, const VARIANT& Password, const VARIANT& WriteResPassword, const VARIANT& ReadOnlyRecommended, const VARIANT& CreateBackup, long AccessMode, const VARIANT& ConflictResolution,
            const VARIANT& AddToMru, const VARIANT& TextCodepage, const VARIANT& TextVisualLayout)
{
      static BYTE parms[] =
            VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
      InvokeHelper(0x11c, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
             &Filename, &FileFormat, &Password, &WriteResPassword, &ReadOnlyRecommended, &CreateBackup, AccessMode, &ConflictResolution, &AddToMru, &TextCodepage, &TextVisualLayout);
}


I am calling it in an implementation file here:

      Worksheets oWorksheets;
      Workbooks oWorkbooks;
      _Worksheet oWorksheet;
      _Workbook oWorkbook;
      Range oRange;
      sLine = "";
      
      CString sItem;
      if (true == m_bHasExcel)
      {
            oWorkbooks = m_oExcelApp.GetWorkbooks();

            oWorkbook = oWorkbooks.Open(m_FileName,vOpt,COleVariant((short)true),
                        vOpt, vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
            
            oWorksheets = oWorkbook.GetWorksheets();
            oWorksheet = oWorksheets.GetItem(COleVariant((short)1));      

            long vLong = 0;
            VARIANT param;
            VARIANT vText;
            param.vt      = VT_BSTR;
            param.bstrVal = SysAllocString(L"c:\\test.txt\0");
        
                              //VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT      VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
            
                               //HERE
                               oWorkbook.SaveAs(param, vText, vText, vText, vText, vText, vLong , vText, vText, vText, vText);



This function call fails

If someone knows another way to do this please let me know how.
0
Comment
Question by:alan93
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11697055
>>This function call fails

Shouldn't that be

          long vLong = 0;
         VARIANT param;
         VARIANT vText;
         param.vt      = VT_BSTR;
         param.bstrVal = SysAllocString(L"c:\\test.txt\0");
         vText.vt = VT_EMPTY; // <<<<<<<<<<<---------------------!!!
       
                             //VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT      VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
         
                              //HERE
                              oWorkbook.SaveAs(param, vText, vText, vText, vText, vText, vLong , vText, vText, vText, vText);

IIRC you need to set the variant to 'empty'.
0
 

Author Comment

by:alan93
ID: 11697094
probably, I changed that but it still fails.

I get: "SaveAs method of Workbook class failed."
0
 

Author Comment

by:alan93
ID: 11697132

I am probably needing a File Format value (2nd param, see void _Workbook::SaveAs above) but don't know what this should be if I want the output to be tab delimited text .  How do you find this out? Its like a hidden mystery of the universe.
0
 
LVL 16

Accepted Solution

by:
nonubik earned 500 total points
ID: 11701282
You can use xlTextwindows as the FileFormat parameter (20) or choose from the common file formats

xlCSV=6
xlCSVMac=22
xlCSVMSDOS=24
xlCSVWindows=23
xlCurrentPlatformText=-4158
xlExcel9795=436
xlTextMSDOS=21
xlTextPrinter=36
xlTextWindows=20
xlUnicodeText=42

oWorkbook.SaveAs(param, COleVariant((long)20), vText, vText, vText, vText, COleVariant((long)1) , vText, vText, vText, vText);
                                                                                                                                      ^
                                                                                                                                       |
                                                                                                                                 //AccessMode: xlNoChange = 1
0
 

Author Comment

by:alan93
ID: 11703780
Thanks, that is the ticket!

However, it errored saying "not able to convert param 7 from COleVariant to Long" but I just changed it to (long)1 and it worked.

thanks a bunch.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now