C#-client using MS Word ?

C

Chris

Hi,

everything works apart from the last line :-((
rng.Value2.ToString()

An exception is thrown : "Old format or invalid type library"
It gets compiled though (so he recognizes the property 'Value2').
So I suppose I'm using a incompatible type lib.

I'm using Excel 2002 : Excel 10.0 Object Library

Any ideas ? Maybe ... using another way to retrieve the value of a cell ?

thnx

Chris
Arne Janning said:
Chris said:
Thanks a lot for that !!!

Chris
Hi Chris,

perhaps I should have been more precise when I wrote about "casting" the
OLEFormat to an Excel.ApplicationClass. This is not as easy as one
might think of. If you search for a solution, you won't find an answer.

http://groups.google.de/groups?q=OLEFormat.Object dotnet

The cast is only possible if you activate the Ole-Object first. Below
please find the full source code for reading an the Value out of cell
"A1" in an Excel-Sheet that has been embedded inside a Word Document:

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Application
Microsoft.Office.Interop.Word.ApplicationClass app = new
Microsoft.Office.Interop.Word.ApplicationClass();
//File we want to open
//Has to be an object for C#
object fileName = @"C:\\test.doc";
//open the file, use VB.NET in the future
DocumentClass doc = (DocumentClass) app.Documents.Open(ref fileName,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss);

//make Word visible
app.Visible = true;

//let's say our Excel-Sheet is the first Object in the document
Microsoft.Office.Interop.Word.OLEFormat ole =
doc.InlineShapes[1].OLEFormat;
string progID = ole.ProgID;
//it won't work without activating the Ole-Object first!!!
ole.Activate();
//for Excel 2003; just for security reasons. One can leave this away
if (progID == "Excel.Sheet.8")
{
//cast the Ole-Object to an Excel.Workkook-Object
Workbook wbk = (Workbook) ole.Object;
//get a reference to the first sheet
Worksheet sht = (Worksheet) wbk.Worksheets[1];
//get Cell "A1"
Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1");
//show the value of "A1"
MessageBox.Show(rng.Value2.ToString());
}
}

You see that some of the Methods in C# look different from what we have
been using in Excel-VBA. Instead of simply using sht.Range("A1") you
have to use Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1")

Some good articles to start Programming Excel with C#:

"HOWTO: Automate Microsoft Excel from Microsoft Visual C# .NET"
http://support.microsoft.com/?id=302084

"HOW TO: Handle Events for Excel by Using Visual C# .NET"
http://support.microsoft.com/?id=302815

"HOW TO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET"
http://support.microsoft.com/?id=307029

"HOWTO: Create an Excel Macro Using Automation from Visual C# .NET"
http://support.microsoft.com/?id=303872

Cheers

Arne Janning
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

What version of Excel are you running this against?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris said:
Hi,

everything works apart from the last line :-((
rng.Value2.ToString()

An exception is thrown : "Old format or invalid type library"
It gets compiled though (so he recognizes the property 'Value2').
So I suppose I'm using a incompatible type lib.

I'm using Excel 2002 : Excel 10.0 Object Library

Any ideas ? Maybe ... using another way to retrieve the value of a cell ?

thnx

Chris
Arne Janning said:
Chris wrote:

Thanks a lot for that !!!

Chris

Hi Chris,

perhaps I should have been more precise when I wrote about "casting" the
OLEFormat to an Excel.ApplicationClass. This is not as easy as one
might think of. If you search for a solution, you won't find an answer.

http://groups.google.de/groups?q=OLEFormat.Object dotnet

The cast is only possible if you activate the Ole-Object first. Below
please find the full source code for reading an the Value out of cell
"A1" in an Excel-Sheet that has been embedded inside a Word Document:

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Application
Microsoft.Office.Interop.Word.ApplicationClass app = new
Microsoft.Office.Interop.Word.ApplicationClass();
//File we want to open
//Has to be an object for C#
object fileName = @"C:\\test.doc";
//open the file, use VB.NET in the future
DocumentClass doc = (DocumentClass) app.Documents.Open(ref fileName,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss);

//make Word visible
app.Visible = true;

//let's say our Excel-Sheet is the first Object in the document
Microsoft.Office.Interop.Word.OLEFormat ole =
doc.InlineShapes[1].OLEFormat;
string progID = ole.ProgID;
//it won't work without activating the Ole-Object first!!!
ole.Activate();
//for Excel 2003; just for security reasons. One can leave this away
if (progID == "Excel.Sheet.8")
{
//cast the Ole-Object to an Excel.Workkook-Object
Workbook wbk = (Workbook) ole.Object;
//get a reference to the first sheet
Worksheet sht = (Worksheet) wbk.Worksheets[1];
//get Cell "A1"
Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1");
//show the value of "A1"
MessageBox.Show(rng.Value2.ToString());
}
}

You see that some of the Methods in C# look different from what we have
been using in Excel-VBA. Instead of simply using sht.Range("A1") you
have to use Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1")

Some good articles to start Programming Excel with C#:

"HOWTO: Automate Microsoft Excel from Microsoft Visual C# .NET"
http://support.microsoft.com/?id=302084

"HOW TO: Handle Events for Excel by Using Visual C# .NET"
http://support.microsoft.com/?id=302815

"HOW TO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET"
http://support.microsoft.com/?id=307029

"HOWTO: Create an Excel Macro Using Automation from Visual C# .NET"
http://support.microsoft.com/?id=303872

Cheers

Arne Janning
 
C

Chris

Hi Nicolas,

I'm using Excel 2002 (10.2614.3501) SP1
Excel 10.0 Object Library

Chris

Nicholas Paldino said:
Chris,

What version of Excel are you running this against?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris said:
Hi,

everything works apart from the last line :-((
rng.Value2.ToString()

An exception is thrown : "Old format or invalid type library"
It gets compiled though (so he recognizes the property 'Value2').
So I suppose I'm using a incompatible type lib.

I'm using Excel 2002 : Excel 10.0 Object Library

Any ideas ? Maybe ... using another way to retrieve the value of a cell ?

thnx

Chris
Chris wrote:

Thanks a lot for that !!!

Chris

Hi Chris,

perhaps I should have been more precise when I wrote about "casting" the
OLEFormat to an Excel.ApplicationClass. This is not as easy as one
might think of. If you search for a solution, you won't find an answer.

http://groups.google.de/groups?q=OLEFormat.Object dotnet

The cast is only possible if you activate the Ole-Object first. Below
please find the full source code for reading an the Value out of cell
"A1" in an Excel-Sheet that has been embedded inside a Word Document:

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Application
Microsoft.Office.Interop.Word.ApplicationClass app = new
Microsoft.Office.Interop.Word.ApplicationClass();
//File we want to open
//Has to be an object for C#
object fileName = @"C:\\test.doc";
//open the file, use VB.NET in the future
DocumentClass doc = (DocumentClass) app.Documents.Open(ref fileName,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss);

//make Word visible
app.Visible = true;

//let's say our Excel-Sheet is the first Object in the document
Microsoft.Office.Interop.Word.OLEFormat ole =
doc.InlineShapes[1].OLEFormat;
string progID = ole.ProgID;
//it won't work without activating the Ole-Object first!!!
ole.Activate();
//for Excel 2003; just for security reasons. One can leave this away
if (progID == "Excel.Sheet.8")
{
//cast the Ole-Object to an Excel.Workkook-Object
Workbook wbk = (Workbook) ole.Object;
//get a reference to the first sheet
Worksheet sht = (Worksheet) wbk.Worksheets[1];
//get Cell "A1"
Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1");
//show the value of "A1"
MessageBox.Show(rng.Value2.ToString());
}
}

You see that some of the Methods in C# look different from what we have
been using in Excel-VBA. Instead of simply using sht.Range("A1") you
have to use Microsoft.Office.Interop.Excel.Range rng =
(Microsoft.Office.Interop.Excel.Range) sht.get_Range("A1", "A1")

Some good articles to start Programming Excel with C#:

"HOWTO: Automate Microsoft Excel from Microsoft Visual C# .NET"
http://support.microsoft.com/?id=302084

"HOW TO: Handle Events for Excel by Using Visual C# .NET"
http://support.microsoft.com/?id=302815

"HOW TO: Transfer XML Data to Microsoft Excel 2002 by Using Visual C#
.NET"
http://support.microsoft.com/?id=307029

"HOWTO: Create an Excel Macro Using Automation from Visual C# .NET"
http://support.microsoft.com/?id=303872

Cheers

Arne Janning
 
A

Arne Janning

Chris said:
I'm using Excel 2002 (10.2614.3501) SP1
Excel 10.0 Object Library

Hi Chris,

Primary Interop Assemblies for Excel 2002 installed or not?

Cheers

Arne Janning
 
A

Arne Janning

For me,

rng.get_Value(miss).ToString()

works as well.

miss was Type.Missing.

Cheers

Arne Janning
 

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