?
Solved

Open .rtf from asp.net page

Posted on 2006-06-01
6
Medium Priority
?
499 Views
Last Modified: 2008-02-01
I need to open a .rtf file when a user clicks on an image button in a datagrid.  Here is my current code:

protected void cmdUDFDownload(object sender, ImageClickEventArgs e)
            {
                  DataGridItem item = ((DataGridItem)((ImageButton)sender).Parent.BindingContainer);
                  DataView dv = Table(TFLDesign).DefaultView;      
                  
                  string fileServerName = AppProperties.getAppProperty("FormLetterServer");
                  string docPath = AppProperties.getAppProperty("FormLetterPath");
                  docPath = docPath.Replace("C:\\","");
                  docPath = docPath.Replace("inetpub\\ftproot\\","");

                  string file = fileServerName.ToString()+docPath.ToString()+dv[item.DataSetIndex]["uf_id"]+"."+dv[item.DataSetIndex]["uf_ext"];
                  file = file.Replace("\\","/");
                  Response.Write("<script>window.open('Controls/Tables/FileExchange/ShowBinaryData.aspx?"+
                        "conttype=Application%2Fmsword&file="+file.Replace(".rtf",".doc")+"','"+FuncCode+"_bd','resizable=yes,toolbar=no,scrollbars=yes,location=no');</script>");
            }

What should I change to get this to work?
0
Comment
Question by:NickPrater
  • 3
  • 3
6 Comments
 

Author Comment

by:NickPrater
ID: 16819772
Here is my updated code:

                  string fileServerName = AppProperties.getAppProperty("FormLetterServer");
                  string docPath = AppProperties.getAppProperty("FormLetterPath");
                  docPath = docPath.Replace("C:\\","");
                  docPath = docPath.Replace("inetpub\\ftproot\\","");

                  string file = fileServerName.ToString()+docPath.ToString()+dv[item.DataSetIndex]["uf_id"]+"."+dv[item.DataSetIndex]["uf_ext"];
                  file = file.Replace("\\","/");
                  
                  Response.Write("<script language=javascript>window.open('" + file.ToString() + "');</script>");

This will now open the window with the correct address but the file is not handled by word like it should be since it is associated.  Also FYI I am loading this file from an FTP site.
0
 
LVL 11

Accepted Solution

by:
JohnModig earned 1600 total points
ID: 16826577
Ýou have to open Word first and then load the document from the program, like this:
-----------------------------

object fileName = docPath;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;

Word.ApplicationClass oWordApp = new Word.ApplicationClass();

Word.Document oWordDoc = oWordApp.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);

oWordDoc.Activate();

-----------------------------
To get above code to work, you have to have a COM reference to the Microsoft Word 10.0 Object Library. Do this in your project by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 10.0 Object Library. Click Select and OK.

In C#, the Word Document class's Open method signature is defined as Open(ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object). What this means is that in C# the Open method takes 15 required arguments, and each argument must be preceded with the ref keyword and each argument must be of type object. Since the first argument is a file name, normally a String value in Visual Basic. NET, we must declare a variable of type object that holds the C# string value, hence the code:

object fileName = docPath;

Note that you have to specify your docPath before this, as you do in your existing code. Although we only need to use the first argument in the Open method, remember that C# does not allow optional arguments, so we provide the final 14 arguments as variables of type object that hold values of System.Reflection.Missing.Value

Regards,
John
Use a template
0
 

Author Comment

by:NickPrater
ID: 16832164
John - thanks so much for your help, I think this solution is going to work for me.  I am however getting an error that states:

The document name or path is not valid. Try one or more of the following: * Check the path to make sure it was typed correctly. * On the File menu, click Open. Search for the file using this dialog box. (ftp://192.168.0.149/files/FormLetters/17)

It seems as if the .rtf is being chopped off the end of the file name even though when I trace the code it seems to be in the object variable.  Could it also be an issue with word opening a file from our FTP?  I'm going to dig some more but if you have any thoughts on this I would love to hear them.

Thanks again.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:JohnModig
ID: 16832572
Well, it appears as you are having problem forming a valid path to the document. I assume you have tried: Response.Write(docPath); just to make sure that this variable is the valid path. Also, make sure that it is so BEFORE the code for open Word executes. To make sure that there are no issues opening the file from your FTP, you can open word manually (using windows startbutton) and then click file > open and type/paste in the full document path. If the file opens without any problem, there has to be an issue with how you form the document path. Try the above and let me know.

John
0
 

Author Comment

by:NickPrater
ID: 16832635
I've read up some more this morning and it appears the way we are trying to open the document is not supported due to security reasons.  I rewrote my code as:

                  FileStream fs = File.OpenRead(file);
                  BinaryReader br = new BinaryReader(fs);
                  char[] buff = br.ReadChars((int)fs.Length);
                  br.Close();
                  fs.Close();

                  Response.ContentType = "Application/msword";
                  Response.Write(buff, 0, buff.Length);
                  Response.End();

Thanks for the help - I can actually use the code you provided for spell checking purposes in another part of the code.
0
 
LVL 11

Expert Comment

by:JohnModig
ID: 16832728
Good. I'm glad to see that you have found a workaround and that you could have some use from the code I provided :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question