Background
Their often is a need to show uploaded Word file contents in a text box. So by considering that requirement I decided to write this article. So let us learn step-by-step how to read a Word file and display its content in a TextBox.
Requirements
This type of requirement can be the result of various scenarios, the most common are as follows:
- To upload a resume and show the contents in a TextBox as summary.
- Save that resume contents into the database so later on it is useful for CV or resume parsing.
- In a blog or community website to upload file contents directly into the editor so it becomes faster to edit contents and save it.
Now let us see the preceding explanation by creating a sample web application as follows:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "ReadWordFilesInFillTextBox" or another as you wish and specify the location.
- Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.
- Drag and drop two Buttons, a Fileuploader and TextBox control onto the <form> section of the Default.aspx page
- Set TextBox text mode to multiline.
Now the default.aspx page source code will looks as follows.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Article by Vithal Wadje</title>
- </head>
- <body bgcolor="navy">
- <form id="form2" runat="server">
- <div style="color: White;">
- <h4>
- Article for C#Corner
- </h4>
- <br />
- <table width="100%">
- <tr>
- <td>
- <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />
- </td>
- </tr>
- </table>
- <br />
- <table>
- <tr>
- <td>
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- <td>
- <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
- </td>
- <td>
- <asp:Button ID="Button1" runat="server" Text="Clear" OnClick="Button1_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
- using System.IO;
- using Microsoft.Office.Interop.Word;
- using System.Text;
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- //createting the object of application class
- Application Objword = new Application();
- //creating the object of document class
- Document objdoc = new Document();
- //get the uploaded file full path
- dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
- // the optional (missing) parameter to API
- dynamic NA = System.Type.Missing;
- //open Word file document
- objdoc = Objword.Documents.Open
- (ref FilePath, ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA
- );
- //creating the object of string builder class
- StringBuilder sb = new StringBuilder();
- for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
- {
- string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
- if (Filedata != string.Empty)
- {
- //Append word files data to stringbuilder
- sb.AppendLine(Filedata);
- }
- }
- //closing document object
- ((_Document)objdoc).Close();
- //Quit application object to end process
- ((_Application)Objword).Quit();
- //assign stringbuilder object to show text in textbox
- TextBox1.Text =Convert.ToString(sb);
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- TextBox1.Text =string.Empty;
- }
- using System;
- using System.IO;
- using Microsoft.Office.Interop.Word;
- using System.Text;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- //createting the object of application class
- Application Objword = new Application();
- //creating the object of document class
- Document objdoc = new Document();
- //get the uploaded file full path
- dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
- // the optional (missing) parameter to API
- dynamic NA = System.Type.Missing;
- //open Word file document
- objdoc = Objword.Documents.Open
- (ref FilePath, ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA, ref NA,
- ref NA, ref NA, ref NA
- );
- //creating the object of string builder class
- StringBuilder sb = new StringBuilder();
- for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
- {
- string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
- if (Filedata != string.Empty)
- {
- //Append word files data to stringbuilder
- sb.AppendLine(Filedata);
- }
- }
- //closing document object
- ((_Document)objdoc).Close();
- //Quit application object to end process
- ((_Application)Objword).Quit();
- //assign stringbuilder object to show text in textbox
- TextBox1.Text =Convert.ToString(sb);
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- TextBox1.Text =string.Empty;
- }
- }
In the preceding UI Browse control will be used to select the files from the physical location. On a upload button click, it will read the uploaded Word file and show it in the TextBox. The Clear button will clear the text box contents.
Now select the Word file and click on upload, it will show the file contents in the TextBox as follows.
Now you have seen how to read Word file contents and put it into a TextBox.
Notes
- Do a proper validation such as if it has a file or not of the File Upload control when implementing.
- For more details and explanation, download the Uploaded Zip file.
Summary
From all the preceding examples you have learned how to read Word files and fill in a TextBox. I hope this article is useful for all readers, if you have a suggestion then please contact me.

Nishant ShahPosted Jun 26, 2019, 3:28 AM
My problem is solve
Nishant ShahPosted May 28, 2019, 2:42 AM
An exception of type 'System.Runtime.InteropServices.COMException' occurred in App_Web_splyak41.dll but was not handled in user code Additional information: Sorry, we couldn’t find your file. Is it possible it was moved, renamed or deleted?.. This kind of exception is coming
Krishna Rajput SinghPosted Sep 24, 2017, 2:15 PM
Also getting error this code Sorry, we couldn’t find your file. Is it possible it was moved, renamed or deleted? ("C:\...\Resume IT Krishna.docx") object missing = System.Type.Missing; Line 25: doc = app.Documents.Open(ref path, Line 26: ref missing, ref missing, ref missing, ref missing, Line 27: ref missing, ref missing, ref missing, ref missing,
Krishna Rajput SinghPosted Sep 24, 2017, 2:15 PM
Hello sir i am getting error this code i need help
joh titusPosted Feb 22, 2017, 8:13 AM
Great Article. Thanks for Sharing. Keep up the good work.
joh titusPosted Feb 22, 2017, 8:12 AM
Great Article. Thanks for Sharing
Vithal WadjePosted Aug 2, 2016, 1:07 PM
Thanks
kalu singh raoPosted Aug 2, 2016, 6:53 AM
Nice
Vithal WadjePosted Jun 21, 2016, 2:48 AM
Omer , If file path is wrong then your solution will work
Omer SelaPosted Jun 9, 2016, 11:10 AM
hi,where i find --"Thats error because of IE. go to IE -Internet option-Security-Custom level setting and find allow local directory path while uploading files and check on enabled checkbox"
Raghav ChaudhariPosted May 11, 2016, 2:38 AM
An exception of type 'System.Runtime.InteropServices.COMException' occurred in App_Web_splyak41.dll but was not handled in user code Additional information: Sorry, we couldn’t find your file. Is it possible it was moved, renamed or deleted?.. This kind of exception is coming
Vithal WadjePosted Aug 1, 2015, 7:15 AM
great ..Raman Gulati Sir
Vithal WadjePosted Jul 29, 2015, 1:05 AM
woh...Thats error because of IE. go to IE -Internet option-Security-Custom level setting and find allow local directory path while uploading files and check on enabled checkbox
Raman GulatiPosted Jul 28, 2015, 4:12 PM
If u have solved Upendra pls help
Raman GulatiPosted Jul 28, 2015, 4:11 PM
Same error is coming for me also. Pls help Vithal Wadje.
Upendra Pratap ShahiPosted Jun 24, 2015, 12:22 AM
from code its give error path C:\...\IIS Express\AWANIT RASUME.doc
Vithal WadjePosted Jun 23, 2015, 10:45 AM
Check your file path
Upendra Pratap ShahiPosted Jun 23, 2015, 6:48 AM
give above error
Upendra Pratap ShahiPosted Jun 23, 2015, 6:44 AM
This file could not be found. ("C:\...\IIS Express\AWANIT RASUME.doc") Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: This file could not be found. ("C:\...\IIS Express\AWANIT RASUME.doc") Source Error: Line 25: Line 26: //open Word file document Line 27: objdoc = Objword.Documents.Open Line 28: (ref FilePath, ref NA, ref NA, ref NA, ref NA, Line 29: ref NA, ref NA, ref NA, ref NA, Source File: d:\upendra\test\AngularJsTest\ReadWordFilesInFillTextBox.aspx.cs Line: 27 Stack Trace: [COMException (0x800a1436): This file could not be found. ("C:\...\IIS Express\AWANIT RASUME.doc")] Microsoft.Office.Interop.Word.Documents.Open(Object& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog, Object& XMLTransform) +0 ReadWordFilesInFillTextBox.btnUpload_Click(Object sender, EventArgs e) in d:\upendra\test\AngularJsTest\ReadWordFilesInFillTextBox.aspx.cs:27 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9633962 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
rama krishnaPosted Jun 15, 2015, 6:02 AM
how i can bind data in to text box like name,exp. etc.. separate textbox from doc file
Vithal WadjePosted Mar 12, 2015, 1:07 PM
yes possible but you need to follow fixed format of your CV
karthik ponkamPosted Mar 11, 2015, 6:10 PM
dude the Post is good but can you get the paticular text into a text box like phone number,name,skills,exp,projects etc into a textbox field
Vithal WadjePosted Mar 7, 2015, 10:25 AM
thanks Rahul Saxena sir
Rahul Kumar SaxenaPosted Mar 7, 2015, 9:33 AM
Good Show....