Friday, March 10, 2006

VS2003: Using Microsoft Word and ASP.NET

It can be a pain using Word documents from an ASP.NET application. Especially because you don’t want to install Office of Word on the server.

The new VSTO 2005 uses Serverdocument to modify Word documents. Nice but then you are bound to Office 2003, and the beauty of a Web interface is that everyone can use it. Also users with Office 2000/XP.

To provide an ASP.NET application to modify Word documents you can use Microsoft’s DSOFile.dll. This DLL simply can modify the document properties of a Word document. For extra functionality you can use good old VBA to read the document properties into fields if neccessary, or you can just insert the documentproperties into the template document.

To use the DSOfile functionality:

- Add a reference to it.
- Make sure you also register the DLL with Regsvr32.

string strFileNameNew = "";
DSOFile.OleDocumentPropertiesClass m_oDocument = null;

//Init variabelen
string strTemplate = System.Configuration.ConfigurationManager.AppSettings["FormTemplate"];

try
{
System.IO.File.Copy(strTemplate, strFileNameNew,true );

m_oDocument = new DSOFile.OleDocumentPropertiesClass();
m_oDocument.Open(strFileNameNew, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);

// Fill document properties
object objParam = “Value to insert”
m_oDocument.CustomProperties["DocProp"].set_Value(ref objParam);


//Document opslaan en sluiten
m_oDocument.Save();
}

finally
{
if (m_oDocument != null)
m_oDocument.Close(true);
}


To add functionality to process the document properties with the fields in the document, use corresponding names and the following VBA code in Document_Open() of your document:

For Each prop In CustomDocumentProperties
For Each frmField In ActiveDocument.FormFields
If frmField.Name = prop.Name Then
frmField.TextInput.Default = prop.Value
End If
Next
Next

ActiveDocument.Fields.Update


To save the values again in the documentproperties (perhaps for importing the Word document again) you place the following VBA code in the Document_Close():

For Each prop In CustomDocumentProperties
For Each frmField In ActiveDocument.FormFields
If frmField.Name = prop.Name Then
prop.Value = frmField.Result
End If
Next
Next

ActiveDocument.Save



You can download DSOfile.dll from Microsoft at:
http://www.microsoft.com/downloads/details.aspx?FamilyId=9BA6FAC6-520B-4A0A-878A-53EC8300C4C2&displaylang=en

4 comments:

Unknown said...

This is very helpful.
Thanks for posting it.

Anonymous said...

Can we use used word interop on web server without having MS Word installed.
I have to create a new word document on the fly on web server(IIS), perform formatting etc and then save the word document on the server. Its all working fine if i have MS word installed on the server. But dosent work otherwise

Running Sasquatch said...

HI, you could use as well xml to build your document. works nice and you don't need to install word etc on your server.

hope that helps

Alex said...

I worked with word document and executed one tool and accidentally my MS Word closed and I lost my data.I must confess I was lucky because I found next tool in community-docx file recovery.Software helped me very fast,it was free as far as I remember,besides that utility can help you and recover your damaged files in Microsoft Word format.