Open Office document using C#

G

Gidi

Hi,

I've dataBase conating office files, I'm able to get them from the dataBase,
but I'm looking for a way to open it for the user, to work normaly (like he
doubled-clicked on the file).

I tried this code:
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new
Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document aDoc =
WordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing);

aDoc.Activate();


The result is that a winWord process is running, but i can't see the document.

another questions is, if there's a general way to open a file (doc,excel,ppt
and so).

Thanks,
Gidi.
 
N

Norman Yuan

Gidi said:
Hi,

I've dataBase conating office files, I'm able to get them from the
dataBase,
but I'm looking for a way to open it for the user, to work normaly (like
he
doubled-clicked on the file).

I tried this code:
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new
Microsoft.Office.Interop.Word.ApplicationClass();


Add this line:

WordApp.Visible=true;

Microsoft.Office.Interop.Word.Document aDoc =
WordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing);

aDoc.Activate();


The result is that a winWord process is running, but i can't see the
document.

another questions is, if there's a general way to open a file
(doc,excel,ppt
and so).


Most "files" created by different applications (Word, Excel, PPT) can only
be opened by its native application. So, the general way is to use the
application to open it, as you have done in your code. Of course you can
open any file and start reading its data in binary format, the catch would
be you have no way to know what the 0s and 1s mean, because the file format
is business secret of the aplications' vender.
 
G

Gidi

Thanks a lot !!!

Norman Yuan said:
Add this line:

WordApp.Visible=true;




Most "files" created by different applications (Word, Excel, PPT) can only
be opened by its native application. So, the general way is to use the
application to open it, as you have done in your code. Of course you can
open any file and start reading its data in binary format, the catch would
be you have no way to know what the 0s and 1s mean, because the file format
is business secret of the aplications' vender.
 
G

Gidi

Hi,

Another question please,

Now that i know how to open and display word document, how can i open and
display excel files and power point files?

Thanks,
Gidi
 
N

Norman Yuan

Excel and PowerPoint are the same as Word as automate-able. So, you do it in
the very similar ways as you do with Word:

1. Add reference to Excel/PowerPoint's object library

2. start Excel/PowerPoint with code

Microsoft.Office.Interop.Excel[PowerPoint].ApplicationClass
ExcelApp[PowerPointApp] = new
Microsoft.Office.Interop.Excel[PowerPoint].ApplicationClass();

ExcelApp[PowerPointApp].Visible=true;

3. open Excel Workbook/PowerPoint Presentation

Excel.Workbook
workBook=ExcelApp.Workbooks.Open("path\myexcel.xls",......);
PowerPoint.Presentation
presentation=PowerPointApp.Presentations.Open("path\mypresetation.ppt",....)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top