473,216 Members | 1,284 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,216 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 3371
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.