using Excel 2003 from C# 2003

M

Mike in Santa Rosa

I'm trying to get a simple c# app built that can launch/manipulate an excel
workbook, sheet. I've chased down several examples and can't any of them to
work. So I must be doing somethnig obviouslt wrong.

Most examples to get things started include something:
using Microsoft.Office.Interop.Excel;
or
using Excel;
or
using Excel = Microsoft.Office.Interop.Excel;

....along with the standard namespaces supplied by visual studio IDE, like
System & System.Drawing, etc.

The examples also say to add a refererence (COM) to my project for microsoft
Excel object lib 9, 10, 11, etc via the Project Add Reference menu pick.

I start with a blank C# windows application project and add my (COM) excel
11 object lib reference(office 2003 installed on my desktop).


Here's what I observe during build for the 3 "using" scenarios:
1) when I build using the (using Microsoft.Office.Interop.Excel;) I get "The
type or namespace name 'Interop' does not exist in the class or namespace
'Microsoft.Office' (are you missing an assembly reference?)

2) when I build using the (using Excel;) I get "Application' is an ambiguous
reference". This refers to the
Applicaiton.Run in my main argument. I can only assume Application in
conflicting with Excel.Application but can't figure out how to straighten
this out.

3) when I build using the (using Excel = Microsoft.Office.Interop.Excel;) I
get a Namespace '' already contains a definition for 'Excel' and can't
continue.

Here's another thing. (with Excel 11 referenced in app)
If I use intellisense to show me the members of Microsoft, there's no
..Office.Interop, only Office.Core. So if I should be using anything in the
"using" section it looks like it should be "using Microsoft.Office.Core;"

What using statement should I be incorporating or what else do I need to do?

I feel that once my "includes" are correct I can do the rest of the excel
stuff.(famous last words)

Thanks for absolutely any response in advance?
Mike
 
S

sloan

Check my blog:
http://spaces.msn.com/sholliday/ 9/22/2005

Its not exactly what your'e looking for, but can probably help.

You don't reference the Interop file, you reference the COM object, and it
creates the Interop.
Its basically an adapter class that lets the two talk to each other.
 
M

Mike in Santa Rosa

OK,
I found the Primary Interop Assembly installation instructions, did that for
Excel, Word & Forms 2.0 and now I can add:

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

and intellisense shows valid members...

but my Application.Run() in my Main is still flagged as ambiguous when I
build.

How do I work around this?

Thanks again, Mike
 
S

sloan

You can only have 1 start up function........

You gotta delete one of them. Without code, its hard to tell.

Basically, there is one entry point to the application........


What I normally do, is a create a class called

Entry.cs

The code looks like this:


using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace MyApplication.Presentation.Winform
{
/// <summary>
/// Main Entry Point.
/// </summary>
public class Entry
{

public static void Main(string[] args)
{
try
{
Application.Run (new MainForm()); // you put the name of your form here
}
catch (Exception ex)
{
MessageBox.Show("Unhandled exception: " + ex.Message);
}
}


}
}


This way, I at least get a MessageBox .. if I don't handle something in the
main application.


...

the vb.net version looks like this;





Imports System
Imports System.Windows.Forms


Namespace EntryPoint
_
'/ <summary>
'/ Summary description for Entry.
'/ This is the main entry point for the application
'/ </summary>
Public Class Entry : Inherits System.Windows.Forms.Form


Public Sub New()
Me.Visible = False
InitializeComponent()
End Sub 'New


Public Overloads Shared Sub Main()
StartApplication(System.Environment.GetCommandLineArgs())
End Sub

Public Shared Sub StartApplication(ByVal args() As String)

Try
Application.Run(New frmMain) ''' put your form here


''This format allows a "catch all" exception block
''This code will only fire when the developer
''doesn't have try/finally/catch blocks



Catch ex As Exception
' Make sure we log the unhandled exceptions.
MessageBox.Show("A serious error has occured. MyApp cannot
continue." & vbCrLf & ex.Message)
'Shut down application
Application.Exit()
End Try
End Sub 'Main


Private Sub InitializeComponent()
'
'Entry
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(248, 69)
Me.Location = New System.Drawing.Point(-1000, -1000)
Me.Name = "Entry"
Me.ShowInTaskbar = False
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Non Visable Startup Window"

End Sub 'InitializeComponent


End Class

End Namespace
 
G

Guest

Try:

System.Windows.Forms.Application.Run()

instead to differentiate betwwent the 'Application' devined within the Exel
interop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top