Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to open word file click on button, only for copy some text on doc file.i have do following code but it opens on browser and it can't show proper way.that's why i thing open only word file on Microsoft word doc,it is the best way,because my use is open doc file for copy some text nothing else.

What I have tried:

C#
string executableLocation = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location);
string xslLocation = Path.Combine(executableLocation, "hemtech changes.docx");
Application ap = new Application();
Document doc = ap.Documents.Open(xslLocation, ReadOnly: false);

Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();
object objWordFile = "D:\\hemtechchanges.docx";
    object objNull = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Document WordDoc = objWordApp.Documents.Open(
    ref objWordFile, ref objNull, ref objNull,
    ref objNull, ref objNull, ref objNull,
    ref objNull, ref objNull, ref objNull,
    ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
    WordDoc.ActiveWindow.Selection.WholeStory();
    WordDoc.ActiveWindow.Selection.Copy();
    string strWordText = WordDoc.Content.Text;
    WordDoc.Close(ref objNull, ref objNull, ref objNull);
    objWordApp.Quit(ref objNull, ref objNull, ref objNull);
    Response.Write(strWordText);
Posted
Updated 3-Aug-16 3:11am
v3
Comments
Richard MacCutchan 30-Jul-16 5:01am    
Where is this application running, and what is the problem?
Vibhusha Devani 1-Aug-16 0:29am    
forgot above code.
application is running on browser.the problem is i have to open word document when i click on button.And open this document for only show some data.
Richard Deeming 3-Aug-16 9:55am    
C# code does NOT run in the browser. C# code runs on the server. It might appear to be running in the browser when you test it from Visual Studio, but that's only because in that specific instance, the server and the client are the same machine. As soon as you deploy your code to a real server, it will stop working.

You need to read the following Microsoft knowledgebase article:
Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

Even if you have Office installed on the server, there are several hoops you have to jump through before you stand any chance of getting your code to work. And even then, there's no guarantee that it won't stop working at some random point in the future.
Vibhusha Devani 4-Aug-16 2:10am    
thank you so much for giving knowledge...!

1 solution

in asp.net;
Maybe it can help
C#
protected void GetFile(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/msword";
    Response.AddHeader("content-disposition", "attachment;filename=xzy.docx");
    Response.TransmitFile(@"c:\test\xzy.docx");
    Response.End();
}
 
Share this answer
 
v2
Comments
ZurdoDev 3-Aug-16 10:07am    
This will not work because the user wants to copy text out of the document programmatically. This suggestion will present the user with the option of saving or opening the file.
Vibhusha Devani 4-Aug-16 2:11am    
Thank You so much,and also i will done it like this,

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = "D:\\hemtechchanges.docx";
Process.Start(startInfo);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900