4 Replies - 445 Views - Last Post: 11 November 2017 - 03:22 PM Rate Topic: -----

#1 DrDress  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-July 12

Excel trouble

Posted 09 November 2017 - 08:11 AM

I'm triyng to open an excel file in C#. I have done this before by copying from external sources. I don't really understand the code. So what I do, is:

1) Add the Excel COM component.

2) Specify:
using Excel = Microsoft.Office.Interop.Excel;



3) I add this to the program:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlRange;

string Path = "C:/PATH/File.xlsx";

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlRange = xlWorkSheet.UsedRange;



But I get a nasty unexplained error when trying to execute
xlWorkBook = xlApp.Workbooks.Open(Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



This is the copied error. I really don't understand whats wrong. As I said I have used this before. But it might have been on a different computer on windows 7. But is a few years ago:

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC
Source=Team Distributor
StackTrace:
at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
at Team_Distributor.Form1.OpenExcel() in C:\Users\permy_000\Dropbox\Per\Programs\Team Distributor\Team Distributor\Form1.cs:line 246
at Team_Distributor.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\permy_000\Dropbox\Per\Programs\Team Distributor\Team Distributor\Form1.cs:line 317
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Button.onclick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Team_Distributor.Program.Main() in C:\Users\permy_000\Dropbox\Per\Programs\Team Distributor\Team Distributor\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Is This A Good Question/Topic? 0
  • +

Replies To: Excel trouble

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Excel trouble

Posted 09 November 2017 - 09:41 AM

The error details aren't particularly helpful. One thing I would immediately try is to use Workbooks.Open with a single filename parameter, omitting all the other, largely optional, ones. (I think IIRC that you may have to specify a version of Missing for some of the arguments.)

Failing this attempt, I would investigate if the values you are supplying for all the arguments are of the appropriate types and values.
Was This Post Helpful? 0
  • +
  • -

#3 GazinAtCode  Icon User is online

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: Excel trouble

Posted 09 November 2017 - 10:25 AM

Have you tried using an alternative such as EPPlus? As far as I can remember, the whole Office Interop thing is not that easy to handle.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Excel trouble

Posted 09 November 2017 - 10:47 AM

To follow up from my previous post, we can just call Workbooks.Open with a filename in C#, link. This, with a basic spreadsheet, will at least confirm whether the arguments are at issue.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Excel trouble

Posted 11 November 2017 - 03:22 PM

I would try reading the docs...

https://msdn.microso...books.open.aspx

and then try something like....


xlWorkBook = xlApp.Workbooks.Open(Path, 0, true, 0, "", "", true, xlWindows, Chr(9), false, true, 0, false ,true, xlNormalLoad);

and check if the workbook is password protected....

This post has been edited by snoopy11: 11 November 2017 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1