473,768 Members | 3,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#-client using MS Word ?

Hi,

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

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******** ******@TK2MSFTN GP09.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.Applicati onClass. 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(o bject sender, System.EventArg s e)
{
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Applicatio n
Microsoft.Offic e.Interop.Word. ApplicationClas s app = new
Microsoft.Offic e.Interop.Word. ApplicationClas s();
//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.O pen(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.Offic e.Interop.Word. OLEFormat ole =
doc.InlineShape s[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.Offic e.Interop.Excel .Range rng =
(Microsoft.Offi ce.Interop.Exce l.Range) sht.get_Range(" A1", "A1");
//show the value of "A1"
MessageBox.Show (rng.Value2.ToS tring());
}
}

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.Offic e.Interop.Excel .Range rng =
(Microsoft.Offi ce.Interop.Exce l.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 3402
Chris,

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

"Chris" <ch********@pan dora.be> wrote in message
news:p3******** **************@ phobos.telenet-ops.be...
Hi,

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

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******** ******@TK2MSFTN GP09.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.Applicati onClass. 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(o bject sender, System.EventArg s e)
{
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Applicatio n
Microsoft.Offic e.Interop.Word. ApplicationClas s app = new
Microsoft.Offic e.Interop.Word. ApplicationClas s();
//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.O pen(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.Offic e.Interop.Word. OLEFormat ole =
doc.InlineShape s[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.Offic e.Interop.Excel .Range rng =
(Microsoft.Offi ce.Interop.Exce l.Range) sht.get_Range(" A1", "A1");
//show the value of "A1"
MessageBox.Show (rng.Value2.ToS tring());
}
}

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.Offic e.Interop.Excel .Range rng =
(Microsoft.Offi ce.Interop.Exce l.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.c om> wrote in
message news:eg******** ******@TK2MSFTN GP11.phx.gbl...
Chris,

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

"Chris" <ch********@pan dora.be> wrote in message
news:p3******** **************@ phobos.telenet-ops.be...
Hi,

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

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******** ******@TK2MSFTN GP09.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.Applicati onClass. 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(o bject sender, System.EventArg s e)
> {
> //C# cannnot handle optional params
> object miss = Type.Missing;
> //new Word.Applicatio n
> Microsoft.Offic e.Interop.Word. ApplicationClas s app = new
> Microsoft.Offic e.Interop.Word. ApplicationClas s();
> //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.O pen(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.Offic e.Interop.Word. OLEFormat ole =
> doc.InlineShape s[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.Offic e.Interop.Excel .Range rng =
> (Microsoft.Offi ce.Interop.Exce l.Range) sht.get_Range(" A1", "A1");
> //show the value of "A1"
> MessageBox.Show (rng.Value2.ToS tring());
> }
> }
>
> 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.Offic e.Interop.Excel .Range rng =
> (Microsoft.Offi ce.Interop.Exce l.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(m iss).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
4754
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 in vb .net application. CODE: Friend WordGlobal_definst As Object
5
6560
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 some variables, consting of a name embraced by special chars, to some value. As for example: change "" to "Michael". Does anybody know whether there is a way for achieving this with basic "file input / output". Can I regard a Word document as...
1
1794
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 me how to achieve my goal. Any suggestions will be appreciated. Thanks and Regards.
0
1076
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 achieve my goal. Any suggestions will be appreciated. Thanks and Regards. Sakharam Phapale
2
1063
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 intermittent. Has anyone else had this problem? Any ideas? I create the class with: Public Class WordCreateLetterClass
4
1157
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 intermittent. Has anyone else had this problem? Any ideas? I create the class with: Public Class WordCreateLetterClass
1
1095
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 manager and utilizing 100% in CPU. How to solve this?. I attached the code which i used in my application. string strVal = "Good"; Word.Application oWord = new Word.Application(); object missingType = Type.Missing;
4
1796
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 following report (using fonts size and others minimum font features): PRODUCT: +++++++++++++++++ SALES.................########.## Provincial taxes....########.## Federal taxes.......########.##
3
5507
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 made before actual printing. Need some way of calling Word as a mail merge with the information from an Access table that the current report uses. I have seen mail merge templates that use something like <<field name>> in them that uses a table....
6
3540
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 11.0 Object library. This adds a few dll's to the \bin directory. When I run the code from visual studio, it works fine. When I copy it to the server (dll's and all), I get the following error when I navigate to the page: Retrieving the COM...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10017
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9961
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9843
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6656
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.