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 6 4086
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.Offic e.Interop.Word;
[...]
//C# cannnot handle optional params
object miss = Type.Missing;
//new Word.Applicatio n
ApplicationClas s app = new 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;
- 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.InlineShape s(/index/).OLEFormat
You can cast this to an Excel.Workbook-Object if you add a reference to
the Excel-Typelibrary.
Excel.WorkbookC lass wbk = doc.InlineShape s(/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
Thanks a lot for that !!!
Chris
"Arne Janning" <sp************ *****@msn.com> wrote in message
news:Ou******** ******@TK2MSFTN GP11.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.Offic e.Interop.Word; [...]
//C# cannnot handle optional params object miss = Type.Missing; //new Word.Applicatio n ApplicationClas s app = new 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;
- 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.InlineShape s(/index/).OLEFormat
You can cast this to an Excel.Workbook-Object if you add a reference to the Excel-Typelibrary.
Excel.WorkbookC lass wbk = doc.InlineShape s(/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
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
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.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
Hi Arne,
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 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
Chris wrote: 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 ?
Hi Chris,
rng.get_Value(m iss).ToString() works as well for me.
Cheers
Arne Janning This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: TLMM |
last post by:
I have a Word doc already created that I want to open from an asp page. I
want it to open in Word and allow the user to modify if necessary. I
currently have it opening (using href), but it is opening in IE. I think I
should be able to do it with javascript to open word and then call that
function on the link to the doc. Can someone give me some pointers?
Thanks in advance.
|
by: Andy Davis |
last post by:
I have a table of data in Access 2002 which is used as the source table for
a mail merge document using Word 2002 on my clients PC. The data is
transferred OK but I've noticed that any dates which fall between
1stday/anymonth/any year and 12thday/anymonth/any year are rearranged in the
wrong format. For example 4th July 2005 from the database would be displayed
as 07/05/2005 in the merged document. In addition blank date fields from the...
|
by: Zeke |
last post by:
I'm using the following code to create word document but the problem is if
you go to task manager you'll see a WINWORD.EXE process is running but not
the application, here is the code:
Word.Document aDoc= WordApp.Documents.Add(ref fileName, ref newTemplate, ref
docType, ref isVisible);
WordApp.Visible = true;
aDoc.Activate();
WordApp.Selection.TypeText("Hello");
|
by: Yuri Vanzine |
last post by:
In asp we can run VBSCRIPT client-side which allows
for 'easy' :?) ms office COM object instantiation. How do
I access a Word object in ASP.NET on the client side?
I would like to do spell checking from a web-based wysiwyg
editor and I am limited to using ms word from the client,
server-side word automation is out of the question for
several important reasons
(http://support.microsoft.com/default.aspx?...
|
by: Yohancef Chin |
last post by:
Hi,
Being fairly new to .NET I am looking for a way to call
MS
Word from an event on a webform, and after the user is
finished save that created document to an SQL Server
database. Has anyone done this? Does it seem possible? I
followed the instructions from a sample on the Microsoft
knowledge base but it only seems to work when creating a
VB.NET Windows .EXE, not an VB.NET ASP app.
| |
by: kbperry |
last post by:
Hi all,
Background:
I need some help. I am trying to streamline a process for one of our
technical writers. He is using Perforce (version control system), and
is constantly changing his word documents, and then converts them to
both .pdf and "Web page" format (to publish to the web). He has a
licensed copy of Adobe Acrobat Professional (7.x).
Questions:
|
by: Mark Rae |
last post by:
Hi,
My client has asked me to provide a "quick and dirty" way to export the
contents of a DataGrid to both Excel for analysis and Word for editing and
printing, so I'm investigating client-side automation.
N.B. the environment is a totally enclosed intranet, all the client machines
have WinXP, IE6 and Office Pro 2003 with all the latest SPs, and the IE
security settings are sufficient to allow the Local Intranet Zone to
instantiate...
|
by: Niyazi |
last post by:
Hi,
I created application that store the data in SQL SERVER that reside on
network. The client also use this application to access the resources
provided with application. But is the client want to register new customer or
companies they will enter the information in Windows Form and the program
automaticaly creates the WORD document under specific folder under
application path. Once the empty word file created than ask user if they want...
|
by: =?Utf-8?B?QmFkaXM=?= |
last post by:
Hi,
I'm trying to follow a mail merging example in C#.Net that I got from:
http://support.microsoft.com/default.aspx/kb/301659
and in one the methods:
Word.Application wrdApp;
Word._Document wrdDoc;
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
|
by: dittytwo |
last post by:
Hi there
The below code works if you don't try to do anything to the word document
which is great (Not :D)
I am having problems with the context command both text (i.e initial document with no text) or a subsequent addition of text, insert, to the document.
as this is going to be used along side other com opened applications Excel power point etc. it needs to work in the self.X orientation as i can get it to work normally see code 2
...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |