By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

C#-client using MS Word ?

P: n/a
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" <sp*****************@msn.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
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=OLE...bject%20dotnet

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

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chris,

What version of Excel are you running this against?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <ch********@pandora.be> wrote in message
news:p3**********************@phobos.telenet-ops.be...
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" <sp*****************@msn.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
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=OLE...bject%20dotnet

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


Nov 16 '05 #2

P: n/a
Hi Nicolas,

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

Chris

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eg**************@TK2MSFTNGP11.phx.gbl...
Chris,

What version of Excel are you running this against?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris" <ch********@pandora.be> wrote in message
news:p3**********************@phobos.telenet-ops.be...
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" <sp*****************@msn.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
> 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=OLE...bject%20dotnet
>
> 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



Nov 16 '05 #3

P: n/a
Chris wrote:
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
Nov 16 '05 #4

P: n/a
Arne Janning wrote:
Chris wrote:
I'm using Excel 2002 (10.2614.3501) SP1
Excel 10.0 Object Library


For me,

rng.get_Value(miss).ToString()

works as well.

miss was Type.Missing.

Cheers

Arne Janning
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.