Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read/write ms office files 1

Status
Not open for further replies.

Naug

Technical User
Sep 24, 2004
85
RU
Hello all, I need to be able to read/write formated text and charts to ms word/excel files (i.e. copy chunk of formated text from excel, store it in db, paste to word perserving format. Same goes for charts). Is it possible to achieve this with C# and if so how (if not possible with C# but you know other solutions please tell anyhow)
Edit/Delete Message
 
There may some 3rd party products to do this, but most people use COM automation to tell the appropriate MS Office component to do the work for them. Do a google on "MS Office Automation" for some code samples. Even if you find VB6 code for doing this, the techniques still apply, just that the syntax will be different since you're in C#.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
couuld you please PLEASE point me to a tutorial? I have been bouncing between 6 forums and most ppl agree that some sort of MS object technology like OLE, COM, ActiveX should do the trick but when I come to the forum that is supposed to deal with that technology I get a forward bounce in best case and silence in worse. And I am just unable to find anything resembling a code example of the thing I need - only commercial third party products and forums that recomend turning excel file to csv ignoring that I need formated text and charts. Sorry for being so emotional but I am starting to think that switching to OO is the only option and assuming that means switching to OO for my non-tech users you must see how desperate I am.

I dont really care about what technology does what I need - Im a javaman myself and it doesnt make much difference (or sense) to me if I use C++ or C.net or C# - Ill have to learn most of it as I go anyhow but I want to see some tangible proof that what I need can be done with that language before I dive in
 
Depending on how complex your requirements are, using C# to do COM automation may be using a sledgehammer to crack a nut. Have you used VBA (Visual Basic for Applications) before? It's a kind of macro language that's built in to Word, Excel, Access and a few others. It's not a 'proper' programming language and isn't really a version of VB - despite the name - but it can do some powerful things.

For instance you can write code in a Word macro to create an instance of an Excel object, open an Excel document, copy an area, paste it into a Word document then close the Excel doc, all without displaying Excel. You can do similar actions between any of the apps that implement VBA.

A good place to start is forum707 and its various FAQs. You invoke the VBA editor from within one of the apps by hitting Alt+F11 and from there you can access fairly comprehensive help files on all of the functions implemented by that app.

Nelviticus
 
I am pretty familiar with VBA. The thing is though I need to store the info in DB from where I will insert it into various files, not just cut/paste between word and excel. I havent found a way to paste formated text into acces either (not to mention the charts)
 
Just messing around, I created a blank Access database and set up a table with a single field of type 'OLE Object'. I copied an Excel chart and pasted it into the first row of the table and hey presto it says Microsoft Excel Chart. If I double-click it it opens in Excel (admittedly, with the entire original multi-tabbed spreadsheet that it was copied from showing on the other tabs).

However, it does show that this is something you can use. An Access database can store 'scrap' objects, which are document chunks that include all the data needed.

I just tried it again with some multi-formatted, differently-highlighted text from Word. This time Access just held the text I'd copied and pasted rather than the whole document. I guess that it stored the whole spreadsheet in the first test because the chart was dynamic, i.e. based on data in the spreadsheet.

It's the 'E' part of OLE that's key here as it stands for 'Embedded'. You can embed objects from any app that supports OLE in any other app that supports OLE so, as I've shown above, you can embed entire or partial Word or Excel objects in an Access database. You can control this quite easily through VBA but I can't help you with the specifics of that because I've never really done it before other than messing around with mail merging.

Hopefully OLE can solve your headaches.

Regards

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top