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

how to use MS Word from client ?

P: n/a
Hi,

- How can I open an existing word document from a C#-client and manipulate
that document from within the C#-client ?

- How can I open an excel document that is embedded in a word-document ?
- How do I read the value of a cell within an Excel document ?

I tried using a Word-OLB file (object library) that I set a reference to but
can't find the right methods I need in order to execute the above.

But instead of importing a COM-library , isn't there another way ... a
typical .NET-way ... by using a specific .NET-class for example or something
?

Any help greatly appreciated.

Thanks

Chris

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


P: n/a
Chris wrote:
- How can I open an existing word document from a C#-client and manipulate
that document from within the C#-client ?
Hi Chris,

this is the way to open a Word-document in C#:

If you have Office XP or 2003 installed get the Primary Interop
Assemblies here:
[Office XP]
http://www.microsoft.com/downloads/d...displaylang=en

[Office 2003]
http://msdn.microsoft.com/library/?u...Assemblies.asp

If you're using Office 2000 you should consider working with late-binding:
http://support.microsoft.com/?kbid=302902

In case you have the PIAs installed you can use this code to open a
word-document:

using Microsoft.Office.Interop.Word;
[...]

//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Application
ApplicationClass app = new 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;
- How can I open an excel document that is embedded in a word-document ?
- How do I read the value of a cell within an Excel document ?

Once you have a reference to a word-document (variable doc in my example
above) you can get a reference to an embedded object by using

doc.InlineShapes(/index/).OLEFormat

You can cast this to an Excel.Workbook-Object if you add a reference to
the Excel-Typelibrary.

Excel.WorkbookClass wbk = doc.InlineShapes(/index/).OLEFormat

The rest is just normal Excel-Programming:

MessageBox.Show(wbk.Cells(1, 1).Value)
I tried using a Word-OLB file (object library) that I set a reference to but
can't find the right methods I need in order to execute the above. But instead of importing a COM-library , isn't there another way ... a
typical .NET-way ... by using a specific .NET-class for example or something
?


As long as Office is based on COM: No.

Cheers

Arne Janning
Nov 16 '05 #2

P: n/a
Thanks a lot for that !!!

Chris

"Arne Janning" <sp*****************@msn.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
Chris wrote:
- How can I open an existing word document from a C#-client and manipulate that document from within the C#-client ?
Hi Chris,

this is the way to open a Word-document in C#:

If you have Office XP or 2003 installed get the Primary Interop
Assemblies here:
[Office XP]

http://www.microsoft.com/downloads/d...displaylang=en
[Office 2003]
http://msdn.microsoft.com/library/?u...Assemblies.asp
If you're using Office 2000 you should consider working with late-binding:
http://support.microsoft.com/?kbid=302902

In case you have the PIAs installed you can use this code to open a
word-document:

using Microsoft.Office.Interop.Word;
[...]

//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Application
ApplicationClass app = new 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;
- How can I open an excel document that is embedded in a word-document ?
- How do I read the value of a cell within an Excel document ?


Once you have a reference to a word-document (variable doc in my example
above) you can get a reference to an embedded object by using

doc.InlineShapes(/index/).OLEFormat

You can cast this to an Excel.Workbook-Object if you add a reference to
the Excel-Typelibrary.

Excel.WorkbookClass wbk = doc.InlineShapes(/index/).OLEFormat

The rest is just normal Excel-Programming:

MessageBox.Show(wbk.Cells(1, 1).Value)
I tried using a Word-OLB file (object library) that I set a reference to but can't find the right methods I need in order to execute the above.

But instead of importing a COM-library , isn't there another way ... a
typical .NET-way ... by using a specific .NET-class for example or something ?


As long as Office is based on COM: No.

Cheers

Arne Janning

Nov 16 '05 #3

P: n/a
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 #4

P: n/a
Chris wrote:
Thanks a lot for that !!!

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=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 #5

P: n/a
Hi Arne,

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
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 #6

P: n/a
Chris wrote:
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 ?


Hi Chris,

rng.get_Value(miss).ToString() works as well for me.

Cheers

Arne Janning
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.