WorkBooks::OpenText file not loading
-
Hi, I have the following code sequences whose pourpose is to display a .txt file in a Excel Spreadsheet, however the .txt file is not being loaded after I execute workbooks.opentext a workbook is loaded but there are no sheets displayed let alone text I get good return codes from IDispatch->invoke and good return codes in excpInfo I have changed the file name to a bogous name and the same thing happens
app.CreateDispatch("Excel.Application"); // this works my code I usally check AX register
Workbooks objBooks(app.Workbooks()); // while testingWorkbooks objBooks = app.GetWorkbooks();
COleVariant FilenameOpen(File), FilenameSave(XlsFile), Origin((short)2), // xlWindows StartRow((short)1), DataType((short)1), // xlDelimited TextQualifier((short)1), // xlDoubleQuote ConsecutiveDelimiter((long)FALSE, VT\_BOOL), Tab((long)FALSE, VT\_BOOL), Semicolon((long)TRUE, VT\_BOOL), Comma((long)FALSE, VT\_BOOL), Space((long)FALSE, VT\_BOOL), Other((long)FALSE, VT\_BOOL), Otherchar((long)FALSE,VT\_BOOL), Fieldinfo((long) FALSE, VT\_BOOL), TextVisualLayout((long)FALSE, VT\_BOOL), DecimalSeperator((long) FALSE, VT\_BOOL), ThousandSepartor((long) FALSE, VT\_BOOL), TrailingMinusNumbers((long) FALSE, VT\_BOOL), Local((long) FALSE, VT\_BOOL), Fileformat((short)33), // xlExcel4 Save((long)FALSE, VT\_BOOL);
COleSafeArray saRet;
DWORD numElements[2];
numElements[0] = 2;
numElements[1] = 2;
saRet.Create(VT_I4,2,numElements);
long index[2];
long val;
index[0]=0;
index[1]=0;
val = 1;
saRet.PutElement(index,&val);
index[0] = 1;
index[1] = 0;
val = 2;
index[0] = 1;
index[1] = 1;
val = 9;objBooks.OpenText((LPCTSTR)"C:\\Program Files\\Microsoft VIsual Studio\\MyProjects\\I46023\\I46023.txt", Origin, COleVariant((long)1),COleVariant((long)1),
1,
ConsecutiveDelimiter, Tab, Semicolon,
Comma,Space,Other,Otherchar,saRet,TextVisualLayout,
DecimalSeperator,ThousandSepartor,TrailingMinusNumbers,
Local);app.SetVisible(true);
app.SetUserControl(TRUE); -
Hi, I have the following code sequences whose pourpose is to display a .txt file in a Excel Spreadsheet, however the .txt file is not being loaded after I execute workbooks.opentext a workbook is loaded but there are no sheets displayed let alone text I get good return codes from IDispatch->invoke and good return codes in excpInfo I have changed the file name to a bogous name and the same thing happens
app.CreateDispatch("Excel.Application"); // this works my code I usally check AX register
Workbooks objBooks(app.Workbooks()); // while testingWorkbooks objBooks = app.GetWorkbooks();
COleVariant FilenameOpen(File), FilenameSave(XlsFile), Origin((short)2), // xlWindows StartRow((short)1), DataType((short)1), // xlDelimited TextQualifier((short)1), // xlDoubleQuote ConsecutiveDelimiter((long)FALSE, VT\_BOOL), Tab((long)FALSE, VT\_BOOL), Semicolon((long)TRUE, VT\_BOOL), Comma((long)FALSE, VT\_BOOL), Space((long)FALSE, VT\_BOOL), Other((long)FALSE, VT\_BOOL), Otherchar((long)FALSE,VT\_BOOL), Fieldinfo((long) FALSE, VT\_BOOL), TextVisualLayout((long)FALSE, VT\_BOOL), DecimalSeperator((long) FALSE, VT\_BOOL), ThousandSepartor((long) FALSE, VT\_BOOL), TrailingMinusNumbers((long) FALSE, VT\_BOOL), Local((long) FALSE, VT\_BOOL), Fileformat((short)33), // xlExcel4 Save((long)FALSE, VT\_BOOL);
COleSafeArray saRet;
DWORD numElements[2];
numElements[0] = 2;
numElements[1] = 2;
saRet.Create(VT_I4,2,numElements);
long index[2];
long val;
index[0]=0;
index[1]=0;
val = 1;
saRet.PutElement(index,&val);
index[0] = 1;
index[1] = 0;
val = 2;
index[0] = 1;
index[1] = 1;
val = 9;objBooks.OpenText((LPCTSTR)"C:\\Program Files\\Microsoft VIsual Studio\\MyProjects\\I46023\\I46023.txt", Origin, COleVariant((long)1),COleVariant((long)1),
1,
ConsecutiveDelimiter, Tab, Semicolon,
Comma,Space,Other,Otherchar,saRet,TextVisualLayout,
DecimalSeperator,ThousandSepartor,TrailingMinusNumbers,
Local);app.SetVisible(true);
app.SetUserControl(TRUE); -
Why are you using the
(LPCTSTR)
cast on your file path? Do you fully understand what a cast does?Use the best guess
Richard This is the function prototype in excel.cpp
oid Workbooks::OpenText(LPCTSTR Filename, const VARIANT& Origin, const VARIANT& StartRow, const VARIANT& DataType, long TextQualifier, const VARIANT& ConsecutiveDelimiter, const VARIANT& Tab, const VARIANT& Semicolon, const VARIANT& Comma,
const VARIANT& Space, const VARIANT& Other, const VARIANT& OtherChar, const VARIANT& FieldInfo, const VARIANT& TextVisualLayout, const VARIANT& DecimalSeparator, const VARIANT& ThousandsSeparator, const VARIANT& TrailingMinusNumbers,
const VARIANT& Local)Filename is Of type LPCTSTR Thanks
-
Richard This is the function prototype in excel.cpp
oid Workbooks::OpenText(LPCTSTR Filename, const VARIANT& Origin, const VARIANT& StartRow, const VARIANT& DataType, long TextQualifier, const VARIANT& ConsecutiveDelimiter, const VARIANT& Tab, const VARIANT& Semicolon, const VARIANT& Comma,
const VARIANT& Space, const VARIANT& Other, const VARIANT& OtherChar, const VARIANT& FieldInfo, const VARIANT& TextVisualLayout, const VARIANT& DecimalSeparator, const VARIANT& ThousandsSeparator, const VARIANT& TrailingMinusNumbers,
const VARIANT& Local)Filename is Of type LPCTSTR Thanks
ForNow wrote:
Filename is Of type LPCTSTR
Then you should provide a
LPCTSTR
in your call, rather than providing aLPCSTR
, and then telling the compiler to ignore the fact that the string may not be in the correct format. Lookup the use of theTEXT()
macro and make sure your strings are generated correctly, and read the documentation on the correct use of casts.Use the best guess
-
ForNow wrote:
Filename is Of type LPCTSTR
Then you should provide a
LPCTSTR
in your call, rather than providing aLPCSTR
, and then telling the compiler to ignore the fact that the string may not be in the correct format. Lookup the use of theTEXT()
macro and make sure your strings are generated correctly, and read the documentation on the correct use of casts.Use the best guess
The Text macro interperts the string if ansi is defined according to the current codepage I think the string I am passing is being interpetted correctly looking at the varaint type of the file it was being interpetted as a VT_BSTR and had the correct value
case VT_BSTR:
{
LPCOLESTR lpsz = va_arg(argList, LPOLESTR);
pArg->bstrVal = ::SysAllocString(lpsz);
if (lpsz != NULL && pArg->bstrVal == NULL)
AfxThrowMemoryException(); -
The Text macro interperts the string if ansi is defined according to the current codepage I think the string I am passing is being interpetted correctly looking at the varaint type of the file it was being interpetted as a VT_BSTR and had the correct value
case VT_BSTR:
{
LPCOLESTR lpsz = va_arg(argList, LPOLESTR);
pArg->bstrVal = ::SysAllocString(lpsz);
if (lpsz != NULL && pArg->bstrVal == NULL)
AfxThrowMemoryException();ForNow wrote:
I think the string I am passing is being interpetted correctly
By luck, not judgement; if your project is built in
UNICODE
mode then it will be wrong. You should not use a cast unless you understand the consequences. Instead, use theTEXT()
macro to ensure that your character string is generated in the appropriate character set to match the build. Your cast in the foregoing code is telling the compiler to ignore the fact that the string may be in the wrong character set.Use the best guess