I want to be able to store a template word document in SharePoint 2007, and use it as a base for spitting out a word doc containing data injected into the template.

I can get the text of my word doc using code as follows:

        SPSite sc = SPContext.Current.Site;
        SPWeb web = sc.AllWebs["MySite"];            

        string contents = web.GetFileAsString("Documents/MyTemplateWord.doc");


Then I can string replace on the "contents" variable. This works fine.

I now want to "open" this new content as a word doc.

My code for this is as follows:

       string attachment = "attachment; filename=MyWord.doc";
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/ms-word";

I'm getting an error though, and not sure how to resolve it.

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near 'ࡱ>

Now clearly it has issues parsing the "string" content.

What am I doing wrong? Is there a different way I should be doing this?


Here is a recent SP.SE post where someone wanted to manipulate a document when it was opened, it might give you some ideas.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.