After struggling to get my word document to act right on my local machine, I understand my server does not have Microsoft Office. I am trying to open, modify, save and display a document. I figured out the OPEN XML SDK 2.0 is the way to go. I just download this program this morning and I don’t know how to integrate this in my code. How do I go about doing this?
I am flying blind, this is what I would like to do. This code works with office
object pathToUse = Server.MapPath("~/Document/TemplateX.docx");
object pathToSave = Server.MapPath("~/Document/FinishedX.docx");
//open file
tempDoc = wordApp.Documents.Open(ref pathToUse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
//Make edits - I had this for my word document
this.FindAndReplace(Application wordApp, "<date>", DateTime.Today.ToShortDateString());
//save
tempDoc.SaveAs(ref pathToSave, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
//return file in my Controller
return File(pathToSave.ToString(), "docx");
I just added code to show I had what was needed but the lack of Microsoft curve ball hurt my mental roll. Thanks in advance. HELP!
OpenXml SDK will let you do that but you may need to take into account special cases that can emerge from editing your template in Word, for example identically styled text being split into more than one elements.
First go and download the Open XML addin for VS2010. It will let you see the structure you’re working with – drag a .docx file in VS2010, look for document.xml inside the package, press Ctrl-K-D to format the document for readability.
Add the DLL(s) that come with the SDK to your project.
Open the Word document from a stream, make sure to copy the template file before that:
Replace text inside tags:
This sample will get you started but expect to see Word structuring its XML in weird ways you didn’t expect, especially if your documents are complex.