Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default vb.net interop

Hi,

While trying to merge CSV file into an Excel workbook (on file per tab),
the data gets truncated to 255 characters either by the cut or the paste
functions. The original CSV file cells contains all the characters while the
resulting XLS file cells are truncated. Any idea why ?

Public Function InsertCSVFilesIntoExcelFile( _
ByVal strSourceFolderPath As String, _
ByVal colCSVFileNames As Common.CCollection, _
ByVal strNewExcelFilePath As String, _
Optional ByVal blnSaveAndCloseExcelFile As Boolean = True) As
OperationResult

Dim oSourceExcel As Excel.Application
Dim oSourceBook As Excel.Workbook
Dim oSourceSheet As Excel.Worksheet
Dim uclsSheet1 As Excel.Worksheet

Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet

Dim f As Excel.XlFileFormat = Excel.XlFileFormat.xlExcel9795
Dim i As Integer
Dim varSavedCursor As Object

Me.ReadProcesses()

If colCSVFileNames.Count = 0 Then Return
CGridImportExport.OperationResult.ResultSuccess

Try
If blnSaveAndCloseExcelFile Then
If Common.FileFunctions.DoesFileExist(strNewExcelFile Path)
Then
Common.FileFunctions.DeleteFile(strNewExcelFilePat h)
End If
End If
' save the crrent screen mouse pointer
varSavedCursor = System.Windows.Forms.Cursor.Current

Catch
Debug.Assert(False)
Err.Clear()
End Try

Try
oSourceExcel = New Excel.Application
oExcel = New Excel.Application
oBook = oExcel.Workbooks.Add()

Try
For i = oBook.Sheets.Count To 2 Step -1
'CType(oBook.Sheets.Item(1), Excel.Worksheet).Delete()
oBook.Sheets.Item(i).Delete()
Next
uclsSheet1 = oBook.Sheets.Item(1)
Catch
Debug.Assert(False)
Err.Clear()
End Try

For i = 1 To colCSVFileNames.Count
If Common.FileFunctions.DoesFileExist(strSourceFolder Path &
colCSVFileNames.Item(i)) Then
oSourceBook =
oSourceExcel.Workbooks.Open(strSourceFolderPath & colCSVFileNames.Item(i))
oSourceSheet = DirectCast(oSourceBook.Sheets.Item(1),
Excel.Worksheet)
oSourceSheet.Cells.Copy()

oBook.Sheets.Add()
oSheet = DirectCast(oBook.Sheets.Item(1), Excel.Worksheet)
oSheet.Name =
Common.StringFunctions.LeftSide(colCSVFileNames.It em(i), ".")
oSheet.Paste()

' Copy a smaller amount of data into the clipboard to
prevent keeping a large amount in memory
oExcel.Range("A1").Select()
oSourceExcel.Range("A1").Copy()
oSourceExcel.DisplayAlerts = False
oSourceBook.Close(False)
oSourceExcel.DisplayAlerts = True
Else
Debug.Assert(False)
End If
'CopyCSVFileToSheet(oSheet, strFolderPath & "\" &
colCSVFileNames.Item(i))
Next

' Now delete sheet1
uclsSheet1.Delete()
If blnSaveAndCloseExcelFile Then
oSourceExcel.DisplayAlerts = False
oBook.SaveAs(strNewExcelFilePath, f)
oSourceExcel.DisplayAlerts = True
End If
Catch ex As Exception
Debug.Assert(False)
Common.ErrorReporting.LogFile.Add(Err.Description)

' restore the screen mouse pointer
System.Windows.Forms.Cursor.Current = varSavedCursor

Return OperationResult.ResultError
End Try


Try
oSheet = Nothing
If blnSaveAndCloseExcelFile Then
oBook.Close(False)
End If
oBook = Nothing

If blnSaveAndCloseExcelFile Then
oExcel.DisplayAlerts = False
oExcel.Workbooks.Close()
oExcel.Quit()
Else
oExcel.Visible = True
End If
oExcel = Nothing

oSourceExcel.DisplayAlerts = False
oSourceExcel.Workbooks.Close()
oSourceBook = Nothing
oSourceSheet = Nothing

oSourceExcel.Quit()
oSourceExcel = Nothing

' restore the screen mouse pointer
System.Windows.Forms.Cursor.Current = varSavedCursor
Catch
Debug.Assert(False)
Err.Clear()
End Try

Me.CleanProcesses()

Return OperationResult.ResultSuccess

End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default vb.net interop

I actually did some more testing and the problem is with the pasting into a
new workbook. I've put a breakpoint right after the cells.copy() and started
pasting the clipboard content into various application and only Excel 2003
was truncating the cells.

"Cell truncation in vb.net interop" wrote:

Hi,

While trying to merge CSV file into an Excel workbook (on file per tab),
the data gets truncated to 255 characters either by the cut or the paste
functions. The original CSV file cells contains all the characters while the
resulting XLS file cells are truncated. Any idea why ?

Public Function InsertCSVFilesIntoExcelFile( _
ByVal strSourceFolderPath As String, _
ByVal colCSVFileNames As Common.CCollection, _
ByVal strNewExcelFilePath As String, _
Optional ByVal blnSaveAndCloseExcelFile As Boolean = True) As
OperationResult

Dim oSourceExcel As Excel.Application
Dim oSourceBook As Excel.Workbook
Dim oSourceSheet As Excel.Worksheet
Dim uclsSheet1 As Excel.Worksheet

Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet

Dim f As Excel.XlFileFormat = Excel.XlFileFormat.xlExcel9795
Dim i As Integer
Dim varSavedCursor As Object

Me.ReadProcesses()

If colCSVFileNames.Count = 0 Then Return
CGridImportExport.OperationResult.ResultSuccess

Try
If blnSaveAndCloseExcelFile Then
If Common.FileFunctions.DoesFileExist(strNewExcelFile Path)
Then
Common.FileFunctions.DeleteFile(strNewExcelFilePat h)
End If
End If
' save the crrent screen mouse pointer
varSavedCursor = System.Windows.Forms.Cursor.Current

Catch
Debug.Assert(False)
Err.Clear()
End Try

Try
oSourceExcel = New Excel.Application
oExcel = New Excel.Application
oBook = oExcel.Workbooks.Add()

Try
For i = oBook.Sheets.Count To 2 Step -1
'CType(oBook.Sheets.Item(1), Excel.Worksheet).Delete()
oBook.Sheets.Item(i).Delete()
Next
uclsSheet1 = oBook.Sheets.Item(1)
Catch
Debug.Assert(False)
Err.Clear()
End Try

For i = 1 To colCSVFileNames.Count
If Common.FileFunctions.DoesFileExist(strSourceFolder Path &
colCSVFileNames.Item(i)) Then
oSourceBook =
oSourceExcel.Workbooks.Open(strSourceFolderPath & colCSVFileNames.Item(i))
oSourceSheet = DirectCast(oSourceBook.Sheets.Item(1),
Excel.Worksheet)
oSourceSheet.Cells.Copy()

oBook.Sheets.Add()
oSheet = DirectCast(oBook.Sheets.Item(1), Excel.Worksheet)
oSheet.Name =
Common.StringFunctions.LeftSide(colCSVFileNames.It em(i), ".")
oSheet.Paste()

' Copy a smaller amount of data into the clipboard to
prevent keeping a large amount in memory
oExcel.Range("A1").Select()
oSourceExcel.Range("A1").Copy()
oSourceExcel.DisplayAlerts = False
oSourceBook.Close(False)
oSourceExcel.DisplayAlerts = True
Else
Debug.Assert(False)
End If
'CopyCSVFileToSheet(oSheet, strFolderPath & "\" &
colCSVFileNames.Item(i))
Next

' Now delete sheet1
uclsSheet1.Delete()
If blnSaveAndCloseExcelFile Then
oSourceExcel.DisplayAlerts = False
oBook.SaveAs(strNewExcelFilePath, f)
oSourceExcel.DisplayAlerts = True
End If
Catch ex As Exception
Debug.Assert(False)
Common.ErrorReporting.LogFile.Add(Err.Description)

' restore the screen mouse pointer
System.Windows.Forms.Cursor.Current = varSavedCursor

Return OperationResult.ResultError
End Try


Try
oSheet = Nothing
If blnSaveAndCloseExcelFile Then
oBook.Close(False)
End If
oBook = Nothing

If blnSaveAndCloseExcelFile Then
oExcel.DisplayAlerts = False
oExcel.Workbooks.Close()
oExcel.Quit()
Else
oExcel.Visible = True
End If
oExcel = Nothing

oSourceExcel.DisplayAlerts = False
oSourceExcel.Workbooks.Close()
oSourceBook = Nothing
oSourceSheet = Nothing

oSourceExcel.Quit()
oSourceExcel = Nothing

' restore the screen mouse pointer
System.Windows.Forms.Cursor.Current = varSavedCursor
Catch
Debug.Assert(False)
Err.Clear()
End Try

Me.CleanProcesses()

Return OperationResult.ResultSuccess

End Function

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increase columns with interop Tim Lypes Excel Programming 2 July 6th 06 12:31 PM
After Save Event (Interop) Jörgen Ahrens Excel Programming 4 February 20th 06 07:58 AM
c# & InterOp & OptionButtons Timo Excel Programming 0 February 13th 06 08:15 AM
Interop problem with VS.NET 2003 Dhruv Excel Programming 0 January 3rd 06 09:52 PM
Interop and VS Behavior Steve B.[_3_] Excel Programming 0 November 25th 05 05:30 PM


All times are GMT +1. The time now is 10:07 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"