473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C#-client using MS Word ?

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
4 3379
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rwalker | last post by:
Error number 429 - COM object with CLSID {000209F0-0000-0000-C000-000000000046} is either not valid or not registered. Intermittent occurance of this error. Using Word.Global to run word macro...
5
by: Michael G. Schneider | last post by:
I know that using Word Automation inside an ASP page is no good idea. Anything I want to do in the current project is: open document, change some text, save and close document. Basically changing...
1
by: Sakharam Phapale | last post by:
Hi All, I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide...
0
by: Sakharam Phapale | last post by:
I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide me how to...
2
by: Helen Trim | last post by:
I have an application that allows users to edit documents using Word 2000. Sometimes it opens Word but the toolbars are missing. Users find this very frustrating. I can't track it down as it is...
4
by: Helen Trim | last post by:
I have an application that allows users to edit documents using Word 2000. Sometimes it opens Word but the toolbars are missing. Users find this very frustrating. I can't track it down as it is...
1
by: Gomathi | last post by:
hi all, In my windows application, i added a word object reference. Using word object, i'm doing spell checking. After spell checking, i quited the word object. But still its running in task...
4
by: Marcel Saucier | last post by:
Is that possible to create the body of a static (or fix) report using Word, saving that report as a RTF file and then loading that file into a RichText Box: Example, with Word, I create the...
3
by: ljungers | last post by:
I need to make some changes to a Query/select/print report using word application. What I need to do is change the way Word is called yet keep the process the same. Word is used so changes can be...
6
by: rwiegel | last post by:
Hi All, I have created an ASP.NET project and for one of the pages I want to pop open a word template and fill in some info. In Visual Studio 2005, I added the COM reference Microsoft Office...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.