Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'System.Runtime.InteropServices.COMException' when Using excel 11.0

Status
Not open for further replies.

vinidel

IS-IT--Management
Jan 23, 2004
78
US
HI,

I have a VB.NET application that converts excel sheets to text file using office XP. It was working fine as expected till my system was upgraded to office 2003.

I have updated the references to excel 11.0 object library and I do not get any build errors. My code runs, it open excel files but it fails when I save it as text.

At this point:

ws.SaveAs(FileName:=txtFileName, FileFormat:=Excel.XlFileFormat.xlTextWindows)


Here is the error message I get:

'System.Runtime.InteropServices.COMException' occurred in XLS2TXT.exe

Additional information: The file could not be accessed. Try one of the following:

• Make sure the specified folder exists.
• Make sure the folder that contains the file is not read-only.
• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *
• Make sure the file/path name doesn't contain more than 218 characters.



Here is the code I am using:

Dim i As Integer
Dim txtFileName() As String

Dim XLApp As New Excel.Application
XLApp = New Excel.Application
Dim ws As Excel.Worksheet

''Hide Excel
XLApp.Visible = True
''Open an Excel workbook
XLApp.Workbooks.Open(XLFilename)
''Turn off annoying messageboxes
XLApp.AskToUpdateLinks = False
XLApp.DisplayAlerts = False

''Save the file as text
i = 0
ReDim txtFileName(i)
txtFileName(i) = "NULL" 'Since VB.NET arry starts from Zero, so defaulting zero location to NULL
For Each ws In XLApp.Worksheets
i = i + 1
ReDim Preserve txtFileName(i)
txtFileName(i) = TempTextFilename & i
'ws.SaveAs(FileName:=txtFileName(i) & ".txt", FileFormat:=Excel.XlFileFormat.xlTextWindows, CreateBackup:=False)
ws.SaveAs(FileName:=txtFileName(i) & ".txt", FileFormat:=Excel.XlFileFormat.xlTextWindows)
Next
'Close the workbook
XLApp.ActiveWorkbook.Close(Savechanges:=False)
''Turn annoying messages back on
XLApp.DisplayAlerts = True
''Quit Excel
XLApp.Quit()
''Kill the variable
XLApp = Nothing



Please help !

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top