Solved

Using Excel9.CPP to convert .xls to .txt

Posted on 2004-08-02
5
652 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

27 Experts available now in Live!

Get 1:1 Help Now