locked
Writing to a Word Document RRS feed

  • Question

  • User43093768 posted
    I know many of you already know this but anyway below is the code which is used to write the text from the TextBox to the word file.

    private void WriteToWordFile()
            {
                // Any folder
                string path = @"C:\ServerFolder\MyWordFile.doc";
               
                string text = TextBox2.Text;
                // Put it in try-catch finally :)
                FileStream fs =  File.Create(path); 
                fs.Close();
                StreamWriter sw = new StreamWriter(path);
                sw.Write(text);
                sw.Close();
           
            }

    Saturday, October 8, 2005 9:23 AM

All replies

  • User-1143650407 posted
    Be nice if it were just as easy to load in the text that I write (instead of getting all the formatting info and the rest).

    Has anyone made a Web app using the TX Text Control? (Edit a word document on the Web)

    http://www.textcontrol.com/applications/wwwwp/


    Monday, October 10, 2005 5:37 PM
  • User43093768 posted
    >>>>> Be nice if it were just as easy to load in the text that I write (instead of getting all the formatting info and the rest).

    I did not understand what you are implying!
    Tuesday, October 11, 2005 10:22 AM
  • User43093768 posted
    what do you mean???
    Tuesday, October 11, 2005 5:35 PM
  • User-1143650407 posted
    1. Make a Word document, using Word and not a Web Form.
    2. Load it in using code like following:

    <code>
            Dim fs As New FileStream(Server.MapPath(Me.txtReadFilename.Text), FileMode.Open, FileAccess.Read)
            Dim tr As New StreamReader(fs)
            Me.txtWord.Text = tr.ReadToEnd

            tr.Close()
            fs.Close()
    </code>

    3. Result:
    ࡱ>    !# q    bjbjt+t+     AA] $sgZ m
    Test word document
    . A!"#$%
    [$@$NormalmH    <A@<Default Paragraph Font 
     (and on and on and on)

    However:

    1. Make a Word document using a Web Form.
    2. Load it in using a Web Form.

    Result:

    Text written: "Hello World".
    Text loaded in: "Hello World".

    There is a different outcome when loading a word document made using Word rather than ASP.NET

    Wednesday, October 12, 2005 5:20 AM
  • User-95607119 posted
    You can use iTextSharp (open-source project, port of Java iText) to generate RTF documents for MS Word.
    Tuesday, October 25, 2005 3:55 AM
  • User1903167306 posted

    i want to read and write from a text file on a web form and how is htis possbile? the code that you gave gives the follwing error!

    how can i fix this problem and create a word document or a text file in the server directory and access it for reading and writing.

    please help me!

     

     

     

    Access to the path "c:\myfile.txt" is denied.

    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.UnauthorizedAccessException: Access to the path "c:\myfile.txt" is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

    Sunday, November 13, 2005 4:33 AM
  • User541108374 posted
    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.UnauthorizedAccessException: Access to the path "c:\myfile.txt" is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

    To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.


    Hi,

    actually the error says it all. ASP.NET doesn't have write privileges to that folder and this is done by default for security reasons. You can give it more permissions but for securitry reasons it's better to create a new windows user account that has the same privileges as the normal aspnet user account but upgraded with the extra to write to that specific folder. After that you need to change your web.config to let that specific application run under the privileges of the new user account.

    This last step can be achieved by setting the impersonation in the <identity> element.


    Grz, Kris.
    Sunday, November 13, 2005 4:57 AM
  • User1204604062 posted
    If you use identity= true it will try to load the file with the current user executing the application if you dont use impersonation it will be executed with aspnet account in windows xp or network service in windows 2003. So you must give permissions to write to that folder for those users.
    Wednesday, November 23, 2005 1:43 PM
  • User-1760310626 posted
    i hav same problem if u find solution than plz send me on gopisjhala28@yahoo.co.in
    Thursday, December 22, 2005 2:20 AM
  • User541108374 posted

    Hi,

    requesting for sending a solution to someone by email only benefits the person who retrieves the email and not the whole community. So please provide solutions on the forums. If you're interested in a certain question but don't want to contribute to it you can always choose the button to send you an email when something is posted to a certain thread to get notified.

    Grz, Kris.

    Thursday, December 22, 2005 3:10 AM